How do I split into Assembly?

4

Good People,

I'm new to assembly, I'm having trouble dividing. When I compile the program at the command line, this error appears Floating point exception .

.section .data

.global op1
.global op2
.global resultado

.section .text

.global Conta    

Account:

prologue

pushl %ebp    
movl %esp, %ebp   

movl op1, %edx
movl op2, %eax
divl %edx, %eax
movl %eax, resultado

epilogue

movl %ebp, %esp  
popl %ebp   

ret
    
asked by anonymous 18.10.2016 / 21:39

1 answer

3

The instruction div , divides the value (unsigned integer) stored in the pair of registers edx:eax (dividend - 64 bits) by the target operator (can be a register or memory location - 32 bits), and stores the quotient in eax and the rest in edx .

As you are storing the value of op1 in edx , and dividing edx:eax by edx (which is part of the dividend), the result of the division is probably greater than 32 bits and this generates the error Floating point exception (or divide error as Intel's manual ).

One possible solution to the problem is to zero the register edx and use a register other than edx and eax to store the divisor (if the division you are trying to do is 32 bits even and not 64 ).

Here is an example of your changed code, using the ebx register to store the divisor:

Conta:
        push %ebp
        movl %esp, %ebp
        movl op2, %ebx       # armazena o divisor em ebx
        xorl %edx, %edx      # zera edx (parte alta do dividendo)
        movl op1, %eax       # armazena o dividendo (parte baixa) em eax
        divl %ebx, %eax      # divide edx:eax por ebx
        movl %eax, resultado
        movl %ebp, %esp
        pop %ebp
        ret

Example run: 13-by-3 split:

Conta () at teste.a:30
30              movl op2, %ebx
(gdb)
31              xorl %edx, %edx
(gdb)
32              movl op1, %eax
(gdb)
33              divl %ebx, %eax
(gdb) info registers
eax            0xd      13      <----- parte "baixa" do dividendo
ecx            0x0      0
edx            0x0      0       <----- parte "alta" do dividendo
ebx            0x3      3       <----- divisor
...
(gdb) s
34              movl %eax, resultado
(gdb) info registers
eax            0x4      4       <----- quociente
ecx            0x0      0
edx            0x1      1       <----- resto
ebx            0x3      3
...
    
19.10.2016 / 07:25