How do I split into Assembly?

Asked

Viewed 2,333 times

4

Boa Pessoal ,

I’m new to Assembly, I’m having trouble with division. When I compile the program on the command line, I get this error Floating point Exception.

.section .data

.global op1
.global op2
.global resultado

.section .text

.global Conta    

Bill:


prelude

pushl %ebp    
movl %esp, %ebp   

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

Plunder

movl %ebp, %esp  
popl %ebp   

ret

1 answer

3


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

How are you storing the value of op1 in edx, and dividing edx:eax for edx (which is part of the dividend), probably the result of the split is greater than 32 bits and this generates the error Floating point Exception (or divide error, as per the intel handbook).

One possible solution to the problem is to reset the register edx and use a register other than edx and eax to store the splitter (in case the split you are trying to do is 32 bit itself and not 64).


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

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 of execution: division of 13 by 3:

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
...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.