简体   繁体   中英

Division overflow on assembly

I'm doing this exercise for university and my assembler returns me an overflow during a division, i did step by step debug checking registers but I can not solve the problem because I'm not able to find it. I post the code so you could try to help me. The goal of the program is to multiply the number of coins for the respective value, for example I have 10 coins of 1 euro, so I have 10 euros; I have to save into memory how much money I have in total; coins and values are in two vectors and they are respectively joined by position.

The code is for an Intel 8086

.model small
.stack
.data
value dw 1,2,5,10,20,50,100,200
coins db 100,23,17,0,79,48,170,211
euro dw 0
cent dw 0
.code
.startup
mov si,0   ;must arrive on 8
mov di,0
mov bx,100 ;to separate euro and decimals
mon: mov al,coins[si]
cbw
mul value[di]
div bx
add cent,dx
add euro,ax
inc si
inc di
inc di
cmp cent,100
jb cicl
mov ax,cent
mov cent,0
mov dx,0
div bx
add euro,ax
mov cent,dx
cicl: cmp si,8
jnz mon
.exit
end

Hope my code is clear. Thank everybody and sorry for my bad english.

I believe cbw is giving the wrong value to AH register, so let's change it by mov ah,0 , here's your code (I'm getting the result 634.11, is that correct?) :

.model small
.stack
.data
value dw 1,2,5,10,20,50,100,200
coins db 100,23,17,0,79,48,170,211
euro dw 0
cent dw 0
.code
.startup
mov si,0   ;must arrive on 8
mov di,0
mov bx,100 ;to separate euro and decimals
mon: mov al,coins[si]
mov ah,0                  ;<========================================= cbw
mul value[di]
div bx
add cent,dx
add euro,ax
inc si
inc di
inc di
cmp cent,100
jb cicl
mov ax,cent
mov cent,0
mov dx,0
div bx
add euro,ax
mov cent,dx
cicl: cmp si,8
jnz mon
.exit
end
 mon: mov al,coins[si] cbw 

You got 2 coins values (170 and 211) that will fault when you use the cbw instruction to extend the value. Better use mov ah,0 . As an alternative you could define the coins array as word and then simply write mov ax,coins[si] . This would of course require you to double inc the SI register and compare SI to 16 in stead of 8 for the loop control.


  cmp cent,100 jb cicl mov ax,cent !? mov cent,0 !? mov dx,0 !? div bx !? add euro,ax !? mov cent,dx !? cicl: 

What you're doing here is way too complex! The div bx instruction (with BX=100) left a remainder of max. 99 in the DX register and so you know that on each iteration the cent variable can at most be raised by 99. Use this fact and simplify the code to:

 cmp cent,100
 jb cicl
 sub cent,100
 inc euro
cicl:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM