Sign in to follow this  

Assembler - reading 32-bit integers

This topic is 3839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I have a school assingment that I need help with. I should make a program that reads large (32-bit) integers from the keyboard. I have made a procedure that reads 16-bit integers that works fine. I just cannot make it work with 32-bit registers. The 16-bit version looks like this: readint PROC mov word ptr [bx],0 ; nollställer talx mov di, bx more: mov ah,1 int 21h ; tecknets ASCII kod finns i al cmp al,0Dh ; Kolla om ENTER tryckts. je done cmp al,30h ; är det >= 0 ? jb error cmp al,39h ; är det <= 9 ? ja error mov ah, 0 sub al, 30h push ax ; sparar det inlästa talet mov ax,[bx] mov bx,10 mul bx jc error ; om CF satt av mul pop dx add ax,dx jc error mov bx, di mov [bx],ax jmp more ; Koppa direkt. done: mov dx,0 ret error: mov dx, 1 ret readint ENDP ...and it is called like this: lea bx, Tal call readint ; Läs in ett heltal mha. underprogrammet readint. cmp dx, 1 ; Kolla om inläsningen misslyckades. je ErrorSection ; Hoppa i så fall till ErrorSection. As you probably have guessed, Tal is where I store the integer, and if dx has value 1 an error has occured and I jump to an error section. Tal is declared like: Tal DW ? Now, I tried to modify the procedure like this: readBigInt PROC far mov dword ptr [ebx], 0 ; nollställer talx mov edi, ebx more: mov ah, 1 int 21h ; tecknets ASCII kod finns i al cmp al, 0Dh ; Kolla om ENTER tryckts. je done cmp al, 30h ; är det >= 0 ? jb error cmp al, 39h ; är det <= 9 ? ja error mov ah, 0 sub al, 30h push eax ; sparar det inlästa talet mov eax, [ebx] mov ebx, 10 mul ebx jc error ; om CF satt av mul pop edx add eax, edx jc error mov ebx, edi mov [ebx], eax jmp more ; Hoppa direkt. done: mov edx, 0 ret error: mov edx, 1 ret readBigInt ENDP Basically I just use DWORDs instead of WORDs and 32-bit registers instead of 16-bit. I call the procedure like: lea ebx, Tal1 call readBigInt cmp edx, 1 ; Kolla om inläsningen misslyckades. je ErrorSection ; Hoppa i så fall till ErrorSection. ... and Tal1 is declared like: Tal1 DD ? However, it does not seem to work. It should jump to the error section whenever a too large number is given or a non-numeric character is given. However, it seems to jump whenever it wants. Any idea what is wrong? Thanks!! //chinc

Share this post


Link to post
Share on other sites
You need to differentiate between 32bit operand size and 32bit address size. Just use 16 bit index registers (bx, di) and lookups (dword ptr [bx]) and you should be fine.

Share this post


Link to post
Share on other sites
Ok, I tried to do the following:

readBigInt PROC
mov dword ptr [bx], 0 ; nollställer talx
mov di, bx
more:
mov ah, 1
int 21h
; tecknets ASCII kod finns i al
cmp al,0Dh ; Kolla om ENTER tryckts.
je done
cmp al,30h ; är det >= 0 ?
jb error
cmp al,39h ; är det <= 9 ?
ja error
mov ah, 0
sub al, 30h
push ax ; sparar det inlästa talet
mov eax, [bx]
mov bx, 10
mul bx
jc error ; om CF satt av mul
pop edx
add eax, edx
jc error
mov bx, di
mov [bx], eax
jmp more ; Hoppa direkt.
done:
mov dx, 0
ret
error:
mov dx, 1
ret
readBigInt ENDP

The procedure does not work any better, though. Any ideas?

I am calling the procedure like:

lea bx, Tal1
call readBigInt
cmp dx, 1 ; Kolla om inläsningen misslyckades.

//chinc

Share this post


Link to post
Share on other sites
Does this work?

readBigInt PROC
mov dword ptr [bx], 0 ; nollställer talx
mov di, bx
more:
mov ah, 1
int 21h
; tecknets ASCII kod finns i al
cmp al,0Dh ; Kolla om ENTER tryckts.
je done
cmp al,30h ; är det &gt;= 0 ?
jb error
cmp al,39h ; är det &lt;= 9 ?
ja error
mov ah, 0
sub al, 30h
push ax ; sparar det inlästa talet
mov eax, dword ptr [bx]
mov ebx, 10
mul ebx
jc error ; om CF satt av mul
xor edx, edx
pop dx
add eax, edx
jc error
mov bx, di
mov dword ptr [bx], eax
jmp more ; Hoppa direkt.
done:
mov dx, 0
ret
error:
mov dx, 1
ret
readBigInt ENDP

lea bx, Tal1
call readBigInt
cmp dx, 1 ; Kolla om inläsningen misslyckades.
je ErrorSection ; Hoppa i så fall till ErrorSection.




Share this post


Link to post
Share on other sites
Yes, it works! Thank you very much.

It still freaks out when giving it very high numbers though. It should return with an error when giving it values of 2 ^ 31 or higher, but it just outputs a lot of random chars on the screen. Otherwise it works fine :)

//chinc

Share this post


Link to post
Share on other sites

This topic is 3839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this