Create a program that prints "You entered a one" if the user enters a 1,
"You entered a two" if the user enters a 2,
"You entered a three" if the user enters a 3.
The program should loop until the user enters a 4, then it should exit.If a number other than 1, 2, 3, or 4 is entered the program should print "You entered an invalid number".
org 0200h
Main:
ldx #InMess1<
ldy #InMess1>
jsr 0E00Ch
jsr 0E009h
jsr 0E015h
CheckOne:
cmp #1d
bne CheckTwo
ldx #OneMess<
ldy #OneMess>
jsr 0E00Ch
jmp Main
CheckTwo:
cmp #2d
bne CheckThree
ldx #TwoMess<
ldy #TwoMess>
jsr 0E00Ch
jmp Main
CheckThree:
cmp #3d
bne CheckFour
ldx #ThreeMess<
ldy #ThreeMess>
jsr 0E00Ch
jmp Main
CheckFour:
cmp #4d
bne Main
CheckError:
cmp #1d>
cmp #4d<
ldx #ErrorMess<
ldy #ErrorMess>
bne Main
brk
InMess1:
dbt 0ah,0dh
dbt "Enter 1-4: "
dbt 0d
OneMess:
dbt 0ah,0dh
dbt "You entered a one. "
dbt 0d
TwoMess:
dbt 0ah,0dh
dbt "You entered a two. "
dbt 0d
ThreeMess:
dbt 0ah,0dh
dbt "You entered a three. "
dbt 0d
ErrorMess:
dbt 0ah,0dh
dbt "You entered an invalid number. "
dbt 0d
end
That is my full code for reference but my main problem lies in the invalid input section (error)
CheckError:
cmp #1d>
cmp #4d<
ldx #ErrorMess<
ldy #ErrorMess>
bne Main
brk
ErrorMess:
dbt 0ah,0dh
dbt "You entered an invalid number. "
dbt 0d
end
Logically, I know how to do this, but I am unaware on how to do this with 6502 assembly language, and my overall question is how exactly do I include all but 4 select numbers for the error check.
You don't really need CheckError
at all. If you reach CheckFour
then the input was something other than 1, 2 or 3. So all you need to do there is:
4
, exit without an error;4
then, given that you reached CheckFour
at all you already know it also wasn't 1
, 2
or 3
. Therefore you know the input wasn't 1
, 2
, 3
or 4
. So print an error, and exit.i.e. something like:
CheckFour:
cmp #4d
beq ValidExit
# If here, then input wasn't 1, 2, 3 or 4.
ldx #ErrorMess<
ldy #ErrorMess>
jsr 0E00Ch
ValidExit:
end
That said, supposing you wanted to do a range check for absolutely any other reason, cmp
sets the negative flag if the result of subtracting the operand from a
is negative. So negative is set if a
was strictly less than the operand.
So e.g.:
lda #4d
cmp SomeValue
bmi SomeValueWasGreaterThanFour
Or:
lda #1d
cmp SomeValue
bpl SomeValueWasLessThanOrEqualToOne
... and consider using carry rather than sign or SBC
if you need to explore a larger range (cf. comments below and Peter Cordes' input).