; div_test.asm print a string using printf ; Assemble: nasm -f elf64 -l div_test.lst div_test.asm ; Link: gcc -m64 -o div_test div_test.o ; Run: ./div_test > div_test.out ; Output: cat div_test.out ; ; rax quotient ; ________ ; divisor / rdx rax 128 bit 2's complement dividend ; rdx remainder ; extern printf ; the C function, to be called %macro pabcd 1 ; a print macro section .text mov rdi, fmt4 ; first arg, format mov [trax], rax ; second arg save users registers mov [trbx], rbx ; third arg mov [trcx], rcx ; fourth arg mov [trdx], rdx ; fifth arg mov rsi, [trax] ; second arg mov rdx, [trbx] ; third arg mov rcx, [trcx] ; fourth arg mov r8, [trdx] ; fifth arg mov rax, 0 ; no xmm used call printf ; Call C function mov rax, [trax] ; second arg restore users registers mov rbx, [trbx] ; third arg being overly cautious mov rcx, [trcx] ; fourth arg mov rdx, [trdx] ; fifth arg %endmacro section .data ; Data section, initialized variables msg: db "Div_Test running", 0 msge: db "Div_Test finished", 0 fmt: db "%s", 10, 0 fmt4: db "rax=%ld, %rbx=%ld, rcx=%ld, rdx=%ld", 10, 0 i31: dq 31 i2: dq 2 section .bss trax: resq 1 trbx: resq 1 trcx: resq 1 trdx: resq 1 section .text ; Code section. global main ; the standard gcc entry point main: ; the program label for the entry point push rbp ; set up stack frame, must be aligned mov rdi, fmt mov rsi, msg mov rax, 0 call printf mov rax, [i31] ; low 64 bits of dividend mov rdx, 0 ; high 64 bits of dividend mov rcx, 0 mov rbx, 0 idiv qword [i2] ; divisor pabcd " " ; quotient in rax, remainder in rdx mov rax, [i31] mov rdx, 0 div qword [i2] pabcd " " mov rax, [i31] mov rdx, 0 ; div 2 ; error ; div qword 2 ; error ; div rax, [i2] ; error ; idiv rax, [i2] ; error mov rbx, 2 idiv rbx ; divisor OK pabcd " " mov rdi, fmt mov rsi, msge mov rax, 0 call printf pop rbp ; restore stack mov rax,0 ; normal, no error, return value ret ; return