I tried using the following codes to get the quotient and reminder for IDIV instruction, but it didn't work as expected. I use NASM as the assembler. It prints "The quotient is: -1840700272The remainder is: 1". I have tried cbq, cbw, etc.. Nothing works.
bits 64
default rel
section .data
quotientMsg db "The quotient is: %d", 0
remainderMsg db "The remainder is: %d", 0
section .bss
reminder resd 1
section .text
extern printf
global main
extern _CRT_INIT
main:
push rbp
mov rbp, rsp
sub rsp, 32
call _CRT_INIT
; Perform signed division
mov rax, -15
mov rcx, 7
xor rdx, rdx ; Clear RDX to zero for the remainder
idiv rcx
; Store the reminder in a reserved space
mov dword [reminder], edx
; Print the quotient
mov rcx, quotientMsg
mov edx, eax
call printf
; Print the remainder
mov rcx, remainderMsg
mov edx, dword [reminder]
call printf
; Exit the program
xor eax, eax
ret
The expected result should be -2 (quotient) and 1 (reminder).