Shmuel Niraev
Shmuel Niraev

Reputation: 81

Dividing in Assembly

I am trying to define a calculator in C language based on the Linux command dc the structure of the program is not so important all you need to know that I get two numbers and I want to divide them when typing /. Therefore, I send this two numbers to an assembly function that makes the division (see code below). But this works for positive numbers only.

When typing 999 3 / it returns 333 which is correct but when typing -999 3 / I get the strange number 1431655432 and also when typing both negative numbers like -999 -3 / I get 0 every time for any two negative numbers.

The code in assembly is:

section .text 
global _div 

_div: 
  push rbp            ; Save caller state 
  mov rbp, rsp 

  mov rax, rdi        ; Copy function args to registers: leftmost... 
  mov rbx, rsi        ; Next argument... 

  cqo
  idiv rbx            ; divide 2 arguments 
  mov [rbp-8], rax 

  pop rbp             ; Restore caller state 

Upvotes: 3

Views: 4473

Answers (2)

jfMR
jfMR

Reputation: 24738

Your issue has to do with how arguments are passed to _div.

Assuming your _div's prototype is:

int64_t _div(int32_t, int32_t);

Then, the arguments are passed in edi and esi (i.e., 32-bit signed integers), the upper halves of the registers rdi and rsi are undefined.

Sign extension is needed when assigning edi and esi to rax and rbx for performing a 64-bit signed division (for performing a 64-bit unsigned division zero extension would be needed instead).

That is, instead of:

mov   rax, rdi       
mov   rbx, rsi 

use the instruction movsx, which sign extends the source, on edi and esi:

movsx rax, edi
movsx rbx, esi

Using true 64-bit operands for the 64-bit division

The previous approach consits of performing a 64-bit division on "fake" 64-bit operands (i.e., sign-extended 32-bit operands). Mixing 64-bit instructions with "32-bit operands" is usually not a very good idea because it may result in worse performance and larger code size.

A better approach would be to simply change the C prototype of your _div function to accept actual 64-bit arguments, i.e.:

int64_t _div(int64_t, int64_t);

This way, the argument will be passed to rdi and rsi (i.e., already 64-bit signed integers) and a 64-bit division will be performed on true 64-bit integers.

Using a 32-bit division instead

You may also want to consider using the 32-bit idiv if it suits your needs, since it performs faster than a 64-bit division and the resulting code size is smaller (no REX prefix):

...   
mov eax, edi      
mov ebx, esi      
cdq
idiv ebx
...

_div's prototype would be:

int32_t _div(int32_t, int32_t);

Upvotes: 5

Michael Petch
Michael Petch

Reputation: 47573

Your comments say you are passing integers to _idiv. If you are using int those are 32-bit values:

extern int _div (int a, int b);

When passed to the function a will be in the bottom 32-bits of RDI and b will be in the bottom 32-bits of RSI. The upper 32-bits of the arguments can be garbage but often they are zero, but doesn't have to be the case.

If you use a 64-bit register as a divisor with IDIV then the division is RDX:RAX / 64-bit divisor (in your case RBX). The problem here is that you are using the full 64-bit registers to do 32-bit division. If we assume for arguments sake that the upper bits of RDI and RSI were originally 0 then RSI would be 0x00000000FFFFFC19 (RAX) and RDI would be 0x0000000000000003 (RBX). CQO would zero extend RAX to RDX. The upper most bit of RAX is zero so RDX would be zero. The division would look like:

0x000000000000000000000000FFFFFC19 / 0x0000000000000003 = 0x55555408

0x55555408 happens to be 1431655432 (decimal) which is the result you were seeing. One fix for this is to use 32-bit registers for the division. To sign extend EAX (lower 32-bit of RAX) into EDX you can use CDQ instead of CQO.You can then divide EDX:EAX by EBX. This should get you the 32-bit signed division you are looking for. The code would look like:

cdq
idiv ebx                 ; divide 2 arguments EDX:EAX by EBX

Be aware that RBX, RBP, R12 to R15 all need to be preserved by your function of you modify them (they are volatile registers in the AMD 64-bit ABI). If you modify RBX you need to make sure you save and restore it like you do with RBP. A better alternative is to use one of the volatile registers like RCX instead of RBX.


You don't need the intermediate register to place the divisor into. You could have used RSI (or ESI in the fixed version) directly instead of moving it to a register like RBX.

Upvotes: 8

Related Questions