Skip to main content
Engineering LibreTexts

13.3: Console Output

  • Page ID
    19941
  • \( \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}}\)

    The system service to output characters to the console is the system write (SYS_write). Like a high-level language characters are written to standard out (STDOUT) which is the console. The STDOUT is the default file descriptor for the console. The file descriptor is already opened and available for use in programs (assembly and high-level languages).

    The arguments for the write system service are as follows:

    Register

    SYS_write

    rax

    Call code = SYS_write (1)

    rdi

    Output location, STDOUT (1)

    rsi

    Address of characters to output

    rdx

    Number of characters to output

    Assuming the following declarations:

        STDOUT        equ     1             ; standard output 
        SYS_write     equ     1             ; call code for write
    
        msg           db      "Hello World"
        msgLen        dq      11 

    For example, to output “Hello World” (it’s traditional) to the console, the system write (SYS_write) would be used. The code would be as follows:

        mov     rax, SYS_write
        mov     rdi, STDOUT
        mov     rsi, msg                           ; msg address
        mov     rdx, qword [msgLen] syscall        ; length value
    

    Refer to the next section for a complete program to display the above message. It should be noted that the operating system does not check if the string is valid.

    Example, Console Output

    This example is a complete program to output some strings to the console. In this example, one string includes new line and the other does not.

    ; Example program to demonstrate console output.
    ; This example will send some messages to the screen.
    
    ; ********************************************** 
    
    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
    
    ; -----
    ;  Define some strings.
    
    message1          db     "Hello World.", LF, NULL
    message2          db     "Enter Answer: ", NULL
    newLine           db     LF, NULL
    
    ;------------------------------------------------------
    
    section     .text 
    global _start 
    _start: 
    
    ; -----
    ;  Display first message.
    
        mov     rdi, message1 
        call    printString
    
    ; -----
    ;  Display second message and then newline
    
        mov     rdi, message2 
        call    printString
        mov     rdi, newLine 
        call    printString
    
    ; -----
    ;  Example program done.
    
    exampleDone:
        mov     rax, SYS_exit 
        mov     rdi, EXIT_SUCCESS 
        syscall
        
    ; ******************************************************
    ;  Generic function 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     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             ; system code for write ()
        mov     rsi, rdi                   ; address of chars to write
        mov     rdi, STDOUT                ; standard out
                                           ; RDX=count to write, set above
        syscall                            ; system call
        
    ; -----
    ;  String printed, return to calling routine.
    
    prtDone:
        pop     rbx
        ret 
        Hello World.
        Enter Answer:_
    

    The newline (LF) was provided as part of the first string (message1) thus placing the cursor on the start of the next line. The second message would leave the cursor on the same line which would be appropriate for reading input from the user (which is not part of this example). A final newline is printed since no actual input is obtained in this example.

    The additional, unused constants are included for reference.


    This page titled 13.3: Console Output is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Ed Jorgensen.

    • Was this article helpful?