6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Apr 27, 2024 12:28 pm

All times are UTC




Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Sat Mar 05, 2016 4:41 pm 
Offline

Joined: Sat Mar 05, 2016 4:27 pm
Posts: 7
In one of my classes we have been learning how to program in assembly language using the 6502 microprocessor. The program is supposed to print "You entered a one." if the user enters a 1, "You entered a two." if the user enters a 2, and "You entered a three." and the program should exit if you enter a 4. I have written the program to do this. The problem arise when I go to run the automatic grading code. The grading code not only inputs the numbers 1-3 but it also attaches an "enter" along with the each number. I have tried to fix this but everything I have done does not work. Also thank you for any help.


Top
 Profile  
Reply with quote  
PostPosted: Sat Mar 05, 2016 6:03 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3346
Location: Ontario, Canada
Welcome, VetteGuy777. How is your program supposed to input a keystroke? Does it read an I/O port, for example, or call a subroutine supplied for this purpose? And, similarly, how is it supposed to output the characters to be printed?

If you show us the program you've written so far then it'll help answer some questions. Right now we don't have enough to go on.

cheers,
Jeff

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Sat Mar 05, 2016 6:22 pm 
Offline

Joined: Sat Mar 05, 2016 4:27 pm
Posts: 7
The program uses subroutines to both accept input and print messages.

OutChar: equ E003h

GetChar: equ E006h

GetCharW: equ E009h

PrntMess: equ E00Ch

OutSpace: equ E00Fh

OutHex: equ E012h

DgtToBin: equ E015h

GetLine: equ E018h

org 0200h

Main:
jsr GetCharW
jsr DgtToBin
sta num
jsr GetCharW

Compare:
ldx num
cpx #1d
beq Message1

cpx #2d
beq Message2

cpx #3d
beq Message3

cpx #4d
beq EndOfProgram

Message1:
ldx #Mess1<
ldy #Mess1>
jsr PrntMess
jmp EndOfProgram
Message2:
ldx #Mess2<
ldy #Mess2>
jsr PrntMess
jmp EndOfProgram
Message3:
ldx #Mess3<
ldy #Mess3>
jsr PrntMess
EndOfProgram:
brk

Mess1: dbt 0ah,0dh
dbt "You entered a one."
dbt 0d


Mess2: dbt 0ah,0dh
dbt "You entered a two."
dbt 0d

Mess3: dbt 0ah,0dh
dbt "You entered a three."
dbt 0d

num: dbt 0d

end

I would also like to point out that we use an emulator for this class. Also everything after a colon should be spaced away from the left edge.


Top
 Profile  
Reply with quote  
PostPosted: Sat Mar 05, 2016 6:52 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3346
Location: Ontario, Canada
There are some issues, but I'm not gonna point them all out. I'm short of time this afternoon, and anyway it's better to give clues rather than actual answers.

Code:
cpx #1d
beq Message1
Does your program branch -- or not -- as you expect it to here? Try single-stepping to find out if the branch occurs. Keep in mind the two values being compared. What is 1d? What is the other value? Do they match? Should they?

BTW I notice there are two similarly-named subroutines. What's the difference between GetChar and GetCharW ? And, did you intend to call GetCharW twice?

Code:
GetCharW: equ E009h
What does the "h" signify? Would it make a difference if it were omitted?

You can bracket your program with the code and /code tags so it'll appear on the forum in monospace font, which most people find easier to read. Use square brackets around the tags.

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Sat Mar 05, 2016 8:15 pm 
Offline

Joined: Sat Mar 05, 2016 4:27 pm
Posts: 7
The "h" I believe has to do with the emulator to distinguish between hexadecimal, decimal, and binary so it does matter. The branches work and the difference between GetChar and GetCharW is that the second one waits for input from the serial port and the other does not and yes I was trying to solve the problem by calling GetCharW twice.


Top
 Profile  
Reply with quote  
PostPosted: Sat Mar 05, 2016 9:00 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3346
Location: Ontario, Canada
VetteGuy777 wrote:
distinguish between hexadecimal, decimal
And a "d" suffix indicates decimal? My bad! :oops: My Forth assembler doesn't use that convention, and I mistook your intent. To me, "1d" (for example) looks like a hex number. I thought you meant 1dh but had forgotten to append the "h".

VetteGuy777 wrote:
The grading code not only inputs the numbers 1-3 but it also attaches an "enter" along with the each number.
VetteGuy777 wrote:
I was trying to solve the problem by calling GetCharW twice.
So the extra GetCharW is to get rid of the "enter"? Probably "enter" is actually two characters -- 0Ah and 0Dh (carriage-return and line-feed; I forget which is which). Maybe you need two extra calls, not one.

Just checking: you're saying the program works alright when you enter keypresses manually, but fails when you run it with run the automatic grading?

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Last edited by Dr Jefyll on Sat Mar 05, 2016 9:18 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Sat Mar 05, 2016 9:14 pm 
Offline

Joined: Sat Mar 05, 2016 4:27 pm
Posts: 7
The program does work when I run it manually but the automatic grading uses this ("send", "1" ~ UnicodeToString(13)) but my program does not know what to do with the "enter" I will try and see if your suggestion works.


Last edited by VetteGuy777 on Sat Mar 05, 2016 9:32 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Sat Mar 05, 2016 9:14 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
What would you do with any other stray input?


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 07, 2016 2:48 pm 
Offline

Joined: Sun Nov 08, 2009 1:56 am
Posts: 387
Location: Minnesota
Quote:
What would you do with any other stray input?


Good question. In general, how does any program you write react to unexpected input?

As written, your program expects to see only four specific input values and has no provision for what to do if the input is not one of those four. It is a very brittle design, IMHO.

Although it is a nuisance that idiot users will not always do the correct thing, you'll have to find a way for your programs to deal with it. In this particular case it might be a good idea to check that the input is one of the four digits you want to react to even before you convert them by calling DigToBin (ie., check their ASCII values, or whatever other encoding convention you are using). If it is not one of them, ignore it and loop around to the start, or print a message to the effect of "unrecognized" and then loop around to start.

'Cause if I was your instructor, I'd throw a "5" into the input just to see what your program would do. I'm mean that way.


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 07, 2016 4:19 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1926
Location: Sacramento, CA, USA
You guys are going to play the "undefined-behavior" card this soon? Well, maybe the task has been more thoroughly defined than we have yet seen, but I would try to get a fragile version working first (to avoid a failing grade). Then a more robust version would follow, if I wasn't backed against the wall time-wise (yeah, right).

Here, there be "Nasal Demons":

http://www.complang.tuwien.ac.at/kps201 ... ion_29.pdf

(page 2, footnote 1)

Mike B.


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 07, 2016 4:36 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
I wasn't so much thinking about undefined behaviour, although it kind of amounts to the same thing. More that the best way to understand what a program is doing is to step through it mentally carefully thinking what happens at each step. In this case, combine what Jeff is saying about how the extra end-of-line characters might appear to the program with thinking about what happens with this branch:
Code:
cpx #4d
beq EndOfProgram


It might be that the problem here is in the challeng of thinking about input character by character, which is how the program necessarily is going to work. It might be that the programmer is thinking line by line, or entry by entry, instead of byte by byte.

Oh, and of course: Welcome VetteGuy777!


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 07, 2016 4:43 pm 
Offline

Joined: Sat Mar 05, 2016 4:27 pm
Posts: 7
I just made it so it will only accept the input of the numbers 1-4. I do not think this is possible but is there a way to retrieve a previous number or something from the A register right after you placed something else in it.


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 07, 2016 6:54 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1926
Location: Sacramento, CA, USA
PHA and PLA are your friends here; you just need to practice how and when to use them.

Mike B.


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 07, 2016 6:58 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
There's a very neat diagram which might be useful: see
viewtopic.php?p=42766#p42766

It might be that you haven't yet ventured into using memory or zero page to store data, VetteGuy77 - if so that's something to think about.


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 07, 2016 9:23 pm 
Offline

Joined: Sat Mar 05, 2016 4:27 pm
Posts: 7
I think I have found out why it is not working but how to fix it is still beyond me. When the automatic grader inputs the test numbers it not only enters the number but it enters the code for "enter" aka carriage return. My program automatically starts to do stuff with the input before an enter command can be given but since it is computerized I believe the test number and enter are being strung together. Thus it does not work. My professor, I think, told me to make it either use or ignore the enter. So does anybody have any ideas.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 27 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: