I am making a program in which I want to take up to a 10 digit number
(4,294,967,296) digit by digit from user and store all of the digits and make
one number of them in EAX.
for example: input = 1 2 3 4, EAX = 1234
i tried with 2 digits for the start and i got some problems and i dont know how
to continue from here. I would really appreciate your help, thanks in advance!!
.model small
.STACK 100h
.data
num dd ?
ten DB 10
.code
.386
start:
MOV AX, @DATA
MOV DS, AX
MOV AH,1
INT 21H
SUB AL,30H
MOV BH,AL
MOV AH,1
INT 21H
SUB AL,30H
MOV CH,AL
MOV AL,BL
MUL ten
ADD AL,CH
push eax
call printNum
MOV AX, 4c00h
INT 21h
END start
MOV AL,BL MUL ten ADD AL,CH
The above would produce the value of your 2-digit number if you would have used the correct register. You have stored the first digit in BH
, but you're using BL
here!
.386 push eax
You are in emu8086. Forget about using 32-bit registers. If you want to work with 32-bit numbers, you'll have to store them in a couple of 16-bit registers. eg DX:AX
where DX
holds the Most Significand Word and AX
holds the Least Significand Word.
To solve your task of building a 10 digit number up to 4GB-1, next codez will be useful:
DI:SI
by 10mov ax, 10
mul si ; LSW * 10 -> DX:AX
mov si, ax
xchg di, dx
mov ax, 10
mul dx ; MSW * 10 -> DX:AX
jc Overflow
add di, ax
jc Overflow
AL=[0,9]
to the dword in DI:SI
mov ah, ah
add si, ax
adc di, 0
jc Overflow
Input:
mov ah, 01h ; DOS.GetKeyWithEcho
int 21h ; -> AL
cmp al, 13 ; Is it <ENTER> ?
je Done
sub al, '0' ; From character to digit
cmp al, 9
ja Invalid
...
jmp Input
Done:
It's important that you decide how you will handle an invalid user input and what you will do for an input that yields a number bigger than 4GB-1. Many programs have been written that overlook this and then at some point mysteriously fail...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.