;; exnl1.asm example print string, integer, float ;;; semicolon, one or more is comment ; sections contain specific information: ; section .data is for initialized variables, strings, arrays, etc. ; section .rodata is for initialized constants, protected, not changeable ; section .bss is block starting symbol for uninitialized arrays, etc. ; section .txt is last, for program instructions, define macros before use ; print using printf (everything same as C) extern printf ; the C function, to be called section .data ; Data section, initialized variables msg: db "nasm exnl1.asm running", 0 ; C string needs 0 msg2: db "exnl1.asm finished", 0 fmt: db "%s", 10, 0 ; The printf format, "\n",'0' string format i: dq 9999 ; integer as quad word fmti: db "i = %d", 10, 0 ; integer format x: dq 37.1 ; floating point as quad word fmtx: db "x = %f", 10, 0 ; floating point format 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 ; print string 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 ; print integer mov rdi,fmti ; address of format, required register rdi mov rsi,[i] ; value of data mov rax,0 ; number of float to print call printf ; Call C function ; print float or double movq xmm0, qword[x] ; value of data mov rdi,fmtx ; address of format, required register rdi mov rax,1 ; number of float to print 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