; exnl6.asm example declare and call funstions and procedures extern printf section .data ; Data section, initialized variables msg: db "nasm exnl6.asm running", 0 ; C string needs 0 msg2: db "exnl6.asm finished", 0 fmt: db "%s", 10, 0 fmti db "diff = %d", 10, 0 fmtf: db "ssq = %f", 10, 0 af: dq 1.1 bf: dq 3.3 ai: dq 13 bi: dq 27 section .bss ; variable and array section cf: resq 1 ci: resq 1 tmp: resq 1 section .text ; Code section. global main main: push rbp ; set up stack frame, must be aligned mov rdi,fmt ; address of format, required register rdi mov rsi,msg ; address of data mov rax,0 ; or can be xor rax,rax call printf ; Call C function call jprt ; call no parameter procedure mov rdi,[ai] ; first integer parameter value mov rsi,[bi] ; second integer paramenter value call diff ; function return in rax mov [tmp],rax ; save result and print result using macro ; print mov rdi, fmti ; address of format string mov rsi, [tmp] ; int value mov rax, 0 ; 0 floating point arguments to printf call printf ; Call C function movq xmm0, qword[af] ; first double parameter value movq xmm1, qword[bf] ; second double parameter value call ssq ; function return in xmm2 movq qword[tmp], xmm2 ; save result and print result ; print mov rdi, fmtf ; address of format string movq xmm0, qword[tmp] ; first floating point in fmt mov rax, 1 ; 1 floating point arguments to printf call printf ; Call C function mov rdi,fmt mov rsi,msg2 mov rax,0 call printf pop rbp ; restore stack mov rax,0 ; normal, no error, return value ret ; return ; function definitions, called above section .data ; Data section jprthi: db "Hi from jprt", 10, 0 section .bss ; variables, arrays space tmpf: resq 1 ; temp space parf1: resq 1 parf2: resq 1 section .text ; Code section. jprt: mov rdi,fmt ; no parameters mov rsi,jprthi mov rax,0 call printf ret ; end jprt diff: mov rax,rdi ; rdi first integer parameter sub rax,rsi ; rsi second integer parameter ret ; function result in rax ssq: movq qword[parf1], xmm0 ; save first parameter movq qword[parf1], xmm1 ; save second parameter fld qword[parf1] ; first parameter fmul qword[parf1] ; squared fstp qword[tmpf] ; saved fld qword[parf2] ; second parameter fmul qword[parf1] ; squared fadd qword[tmpf] ; sum fstp qword[tmpf] ; return value movq xmm2, qword[tmpf] ; return in register ret