A
A
Alexander Knyazev2016-04-24 23:57:51
Programming
Alexander Knyazev, 2016-04-24 23:57:51

Division does not work in assembler. What have I done wrong?

I'm trying to first divide the first two elements of the arr16 array by the first element of the arr8 array, then the third and fourth element of arr16 by the first element of the arr8 array, the first division goes well, but when adding the second division to the code, it compiles, but the executable does not start and does not output anything :

.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
include <\masm32\include\debug.inc>
includelib <\masm32\lib\debug.lib>

.data

num1 dd ?
ost1 dd ?
num2 dd ?
ost2 dd ?
num3 dd ?
num4 dd ? 
arr16 dd 0000b, 0001b, 0010b, 0100b ;массив - делимое 16 байт
arr8 dd 0001b, 0001b ; массив - делитель 8 байт
; Деление:
;   в роли делимого выступает пара регистров EDX:EAX - если делитель имеет размерность двойное слово (4 байта), 
;   тогда после деления частное находим в регистре EAX, 
;   остаток от деления - в регистре EDX.
.code
start:
mov edx, [arr16]    ; заносим в edx первый элемент массива
mov eax, [arr16+4]  ; заносим в eax второй элемент массива
div [arr8]          ; делим число edx:eax на первый элемент массива с делителем
mov num1,eax        ; после деления записываем в num1 частное
mov ost1, edx       ;  а в ost1 записываем остаток от деления
add [arr16+8], edx  ; добавляем остаток к третьему элементу массива- делимого
xor eax,eax         ; очищаем eax
xor edx,edx         ; очищаем edx
mov edx, [arr16+8]  ; третий элемент
mov eax, [arr16+12] ; чтвертый элемент
div [arr8]
;mov num2, eax
;mov ost2, edx
PrintLine
PrintDec num1
PrintLine
invoke ExitProcess, 0
end start

What have I done wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2016-04-25
@jcmvbkbc

mov edx, [arr16] ; put the first element of the array into edx
mov eax, [arr16+4] ; put the second element of the array into eax

Let's start with the fact that you need to put the high part of the dividend in edx, and the low part in eax, and on x86 words are written to memory with the low part forward (little endian).
If the result of the division does not fit in eax, you will get the #DE exception, and when dividing the number 0x0000000100000000 (namely, you divide it by loading it into eax 0 and into edx 1) by 1, the result will definitely not fit into 32 bits.
In general, looking at your questions, it seems that you want to implement long arithmetic in assembler. But long division in the way you're trying to do doesn't exactly work. You need to either divide bit by bit "in a column", or approximate by some number (or some other way, as it did not occur to me).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question