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