Skip to main content
Engineering LibreTexts

16.4: Assembly Language Example

  • Page ID
    19963
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    An example assembly language program to read and display the command line arguments is included for reference. This example simply reads and displays the command line arguments.

    ;  Command Line Arguments Example
    ; -------------------------------------------------------
    
    section .data 
    
    ; -----
    ; Define standard constants. 
    LF           equ     10                ; line feed
    NULL         equ     0                 ; end of string
    TRUE         equ     1
    FALSE        equ     0 
    EXIT_SUCCESS equ     0                 ; success code
    
    STDIN        equ     0                 ; standard input
    STDOUT       equ     1                 ; standard output
    STDERR       equ     2                 ; standard error
    
    SYS_read     equ     0                 ; read
    SYS_write    equ     1                 ; write
    SYS_open     equ     2                 ; file open
    SYS_close    equ     3                 ; file close
    SYS_fork     equ     57                ; fork
    SYS_exit     equ     60                ; terminate
    SYS_creat    equ     85                ; file open/create
    SYS_time     equ     201               ; get time
    
    ; -----
    ;  Variables for main.
    
    newLine db LF, NULL
    ; ------------------------------------------------------
    section .text global main main:
    ; -----
    ;  Get command line arguments and echo to screen.
    ;  Based on the standard calling convention,
    ;    rdi = argc (argument count)
    ;    rsi = argv (starting address of argument vector)
    
        mov    r12, rdi            ; save for later use...
        mov    r13, rsi           
    
    ; -----
    ;  Simple loop to display each argument to the screen.
    ;  Each argument is a NULL terminated string, so can just
    ;  print directly.
    
    printArguments:
        mov     rdi, newLine 
        call    printString     
        
        mov     rbx, 0
    printLoop:
        mov     rdi, qword [r13+rbx*8] 
        call    printString
    
        mov     rdi, newLine call printString
        inc     rbx
        cmp     rbx, r12 
        jl      printLoop
        
    ; -----
    ;  Example program done.
    
    exampleDone:
        mov     rax, SYS_exit 
        mov     rdi, EXIT_SUCCESS 
        syscall
    
    ; **********************************************************
    ;  Generic procedure to display a string to the screen.
    ;  String must be NULL terminated.
    ;  Algorithm:
    ;  Count characters in string (excluding NULL)
    ;  Use syscall to output characters 
    ;  Arguments:
    ;      1) address, string
    ; Returns:
    ;      nothing
    
    global printString printString:
        push    rbp
        mov     rbp, rsp 
        push    rbx
    ; -----
    ;  Count characters in string.
    
        mov     rbx, rdi
        mov     rdx, 0 
    strCountLoop:
        cmp     byte [rbx], NULL 
        je      strCountDone   
        inc     rdx
        inc     rbx
        jmp     strCountLoop
    strCountDone:
    
        cmp     rdx, 0 
        je      prtDone
    
    ; -----
    ;  Call OS to output string.
      
        mov    rax, SYS_write                ; code for write ()
        mov    rsi, rdi                      ; addr of characters
        mov    edi, STDOUT                   ; file descriptor
                                             ; count set above
        syscall                              ; system call
        
    ; -----
    ;  String printed, return to calling routine.
    
    prtDone:
        pop     rbx
        pop     rbp 
        ret
        
    ; *******************************************************
    

    The printString() function is repeated in this example and is unchanged from the previous examples.

    It must be noted that in order for this to work, the program should be assembled as usual but linked with the GNU C compiler, either GCC or G++.

    For example, assuming this example program is named cmdLine.asm, the assembly and linking would be as follows:

           yasm -g dwarf2 -f elf64 cmdLine.asm -l cmdLine.lst
           gcc -g -o cmdLine cmdLine.o
    

    Note, Ubuntu 18 will require the no-pie option on the gcc command as shown:

           gcc -g -no-pie -o cmdLine cmdLine.o

    If the standard linker is used, the arguments will not be passed in the correct manner.


    This page titled 16.4: Assembly Language Example is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Ed Jorgensen.

    • Was this article helpful?