NASM Coding: Basics - Program Examples

  1. ; The string "6-7" will be printed 5 times on the screen (using a loop):
    
    section .data
        message db "6-7", 10    ; message = "6-7\n"
        msg_len equ $ - message ; msg_len = len(message)
    
    section .text
        global _start
    
    _start:
        mov rbx, 5        ; Put 5 in rbx
    
    loop_start:           ; The loop_start label
        dec rbx           ; Decrement rbx -= 1
        mov rax, 1        ; Put the 'write' syscall code in rax
        mov rdi, 1        ; File descriptor: stdout (screen)
        mov rsi, message  ; Use the content in 'message'
        mov rdx, msg_len  ; length
        syscall
        
        cmp rbx, 0        ; Check if (rbx == 0)
        jne loop_start    ; If not equal, jump back to loop_start
    
        mov rax, 60       ; Put the 'exit' syscall code in rax
        mov rdi, 0        ; Put 0 in rdi (1st argument to _exit)
        syscall           ; Call _exit(rdi), which is _exit(0)