I have this homework that requires me to ask for two integers from the user, let them choose between addition or subtraction and then display the result. One of the requirements for the assignment is that you must send an error message if the input is invalid. For example, if instead of a number you input a letter or any other character, or if you input any character other than '+' or '-'.
I tried to compare the input to each digit(which I know is not ideal and uses more memory, but couldn't think of anything else) but it didn't work.
INPUT OF NUMBER:
mov dx,offset MSG_1
mov ah,09h
int 21h
mov bx,offset VALUE_1
call STRING_INPUT
mov bx,offset OUT_DATA_1
call STRING_MOVE
STRING_INPUT
proc near
mov ah,0Ah
mov dx,offset IN_BUF
int 21h
mov al,byte ptr[IN_BUF + 1]
cmp al,0
jbe ST_IN_ERR
mov al,byte ptr[IN_BUF + 1]
mov ah,00h
mov si,ax
mov word ptr[bx],0
mov word ptr[UNIT],1
STRING_INPUT_LOOP:
cmp si,0
je STRING_INPUT_NEXT
mov al,byte ptr[IN_BUF + 1 + si]
sub al,30h
mov ah,0
mul word ptr[UNIT]
add word ptr[bx],ax
dec si
mov ax,word ptr[UNIT]
mul word ptr[TEN]
mov word ptr[UNIT],ax
jmp STRING_INPUT_LOOP
STRING_INPUT_NEXT:
ret
STRING_INPUT
endp
The program is supposed to send an error if the input is any character other than a digit, but when I input a letter, it transform that letter into a value(which I suspect is the hexadecimal value or something of the sorts) and continues as normal. In the code above the part where i tried to compare the value to each digit is not included because I deleted it in a fit of frustration.
sub al, '0'
thencmp al,9
/ja non_digit
. – Peter Cordes Jul 31 '19 at 6:06AL
is ordered group of eight bits. I.e. it can't contain "letter" or "hexa value", that's interpretation of the bit pattern stored in AL. Because computers don't have means to store "letters" as letters, the SW does use some kind of encoding particular letter as particular bit pattern, in ASM tutorials usually the ASCII encoding is used (it's simple), i.e. hexa value 0x61, binary 0b01100001 and letter 'a' are just different interpretations of the same value. – Ped7g Jul 31 '19 at 12:481234
, you receive in the buffer reserved for 0x0A service five bytes: 0x31, 0x32, 0x33, 0x34, 0x0D (the last one is the "Enter" key, the first four are ASCII encoding of digits 1, 2, 3 and 4). So to create something like "calculator" you have to convert that "string" input into native integer form (16 bits pattern probably makes most sense for a task like this, if you are in 16b real mode of the CPU ... 1234 in binary/bits is0100'1101'0010
). – Ped7g Jul 31 '19 at 12:52