Reputation: 149
Program should accept a character and an odd number (max 21).
My problem is that it does not seem to accept inputs properly as it always jump to the show error part. Whats wrong with my code?
.model small
.stack
.386
.data
msg db "Input a character and an odd number(max 21): $"
msg1 db 10,13,"ERROR: Input an odd number number after the character", 10, 13, '$'
msg2 db 10,13, "ERROR: Number is not an odd number", 10, 13, '$'
inp db ?,?,?
.code
main proc far
again: mov ax,@data
mov ds, ax
mov ah, 9
lea dx, msg
int 21h
lea dx, inp
mov ah, 0Ah
int 21h
;*********number check******
mov bl, inp+1
or bl, 30h ; add 30h
cmp bl,30h ; '0'
jnge notnum ; <-------PROGRAM ALWAYS GO HERE FOR ANY INPUT I MAKE
cmp bl,39h ; '9'
jnle notnum
mov bl, inp+2
cmp bl, 00h
je numIsOneDigit
or bl, 30h
cmp bl,30h ; '0'
jnge notnum
cmp bl,39h ; '9'
jnle notnum
;***************************
;****if odd number check****
;numIsTwoDigit
mov cl, inp+2
shr cl, 1
jnc notOdd
jmp drawDiamond
numIsOneDigit:
mov ch, inp+1
shr ch, 1
jnc notOdd
jmp drawDiamond
;***************************
;********ERRORS*************
notnum:
mov ah, 9
lea dx, msg1
int 21h
jmp again
notOdd:
mov ah,9
lea dx, msg2
int 21h
jmp again
;************************
drawDiamond:
;some code here
exit:
mov ah, 4ch
int 21h
main endp
end main
Upvotes: 2
Views: 2065
Reputation: 9899
inp db ?,?,?
Since you want to use the DOS input function 0Ah you need to provide the correct input structure. You defined this structure to only have 3 uninitalized bytes, but DOS expects the first byte to hold the buffer length and the second byte to be reserved so it can return you the number of characters that were actually inputted.
Knowing that you want to input a maximum of 3 characters, the correct structure is:
inp db 4,0,4 dup (0)
It's 4 (not 3) because DOS will also append a carriage return code (13) at the end of the input string. This extra byte will not be included in the count you recieve in the second byte!
Having corrected this, you'll need to change the rest of the program also!
;*********number check****** mov bl, inp+1 or bl, 30h ; add 30h cmp bl,30h ; '0'
The first digit (if any) will be at inp+3.
The second digit (if any) will be at inp+4.
These digits are already characters, so I don't understand why you wrote or bl, 30h
several times.
mov bl, inp+2 cmp bl, 00h je numIsOneDigit
If this compare wants to check if a second digit is available then you need to compare with 13 (not with 0).
Upvotes: 2