Reputation: 11
How am I supposed to get more division numbers in Assembly?
I can only print the first decimal number :c
org 100h ;start
xor edx,edx
mov eax,1
mov ebx,7
div ebx ; 1 / 7
; EAX / EBX = EAX; remainder IN EDX
; EDX VALUE IS ONLY 1 INSTEAD OF 1428571428571429....
; SO instead of printing 0,1428571428571429...x
;(x is how many decimal numbers I want) I can only print out 0,1 :(
mov ax,4c00h;end
int 21h
Upvotes: 1
Views: 596
Reputation: 39411
The integer division div
looses the fraction from calculating 1/7 because it can only store the integer part of the result in the EAX
register.
It is possible to print the result from calculating 1/7 with 9 decimal places and still only use integer operations. Just scale it up a bit and rather calculate 1000000000/7.
ORG 256
mov eax, 1000000000
mov ebx, 7
xor edx, edx
div ebx ; -> EAX = 142857142 EDX=6
; Rounding to nearest
shr ebx
cmp ebx, edx
adc eax, 0
; Converting to decimal characters
mov ebx, 10
push bx ; Sentinel
NextDiv:
xor edx, edx
div ebx
add dl, '0'
push dx
test eax, eax
jnz NextDiv
; Printing the result
mov ah, 02h ; DOS.PrintChar
mov dl, '0'
int 21h
mov dl, '.'
int 21h
pop dx
NextChar:
int 21h
pop dx
cmp dx, bx
jne NextChar
; Giving yourself an opportunity to see the result
mov ah, 00h ; BIOS.GetKey
int 16h
; Quiting the program
mov ax, 4C00h ; DOS.Terminate
int 21h
The above program will print:
0.142857143
Upvotes: 2