i seem to have hit a wall and cannot find any examples as to how about this. I need to convert ascii characters into a number, i.e the user types 150 + 123
, so I have to read the first number in ascii, then convert it to dec by subtracting the 0 from each digit, but here is the problem. I don't know how to actually create a number out of those digits since the user can input 1 digit or 5. Please ignore the foreign comments in the code. Thank you
.model small
.stack 100h
.data
prompt1 db "Please enter an action: ", 10, 13, "$"
prompt2 db "Answer: ", 10, 13, "$"
newline db 10, 13, '$'
buffer db 255 ; Denotes the number of maximal symbols hope
sizeread db 0 ; It will be available on how many characters scanned
buffer_data db 255 dup (?) ; It will be hosted on-line scanned data to fill 255 characters $
.code
start:
mov dx, @data ;
mov ds, dx ;
print_prompt:
mov ah, 9
mov dx, offset prompt1 ; Messages placed in the DX register address
int 21h ; welcome petraukimą
ret
input:
mov ah, 0Ah
mov dx, offset buffer
int 21h
ret
number:
xor bx,bx
xor ax,ax
mov al, 10 ;set al to 0 so i can MUL
mov cx, 5
loopstart: ;start the cycle
cmp [buffer_data+bx], ' ' ;check if I reached a space
je space ; jump to space (the space func will read the + or - sign later on)
mov bx, [bx+1] ; bx++
;Now i got no idea how to go about this....
space:
end start
You need to multiply the accumulator by 10 (assuming your input is in decimal, i.e. if hex multiply by 16):
xor ax, ax ; zero the accumulator
mov cx, 10 ; cx = 10
input:
movzx bx, <next input char from left to right>
cmp bl, '\n' ; end of input
je out
sub bl, '0' ; convert ascii char to numeric value
mul cx ; ax = ax*10, prepare for next digit (assuming result fits in 16 bits)
add ax, bx ; ax = ax + bx, add current digit
jmp input
out:
On reaching 'out' cx has the numeric value of the input number.