Skip to main content
Engineering LibreTexts

10.3: Implement the Program

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

    Based on the algorithm, a program can be developed and implemented. The algorithm is expanded and the code added based on the steps outlined in the algorithm. This allows the programmer to focus on the specific issues for the current section being coded including the data types and data sizes. This example addresses only unsigned data so the unsigned divide (DIV, not IDIV) is used. Since the integer is a double-word, it must be converted into a quadword for the division. However, the result and the remainder after division will also be a double-words. Since the stack is quadwords, the entire quadword register will be pushed. The upper-order portion of the register will not be accessed, so its contents are not relevant.

    One possible implementation of the algorithm is as follows:

    ;  Simple example program to convert an
    ;  integer into an ASCII string.
    
    ; *********************************************************
    ;  Data declarations
    
    section .data 
    
    ; -----
    ;  Define constants
    
    NULL                equ    0
    EXIT_SUCCESS        equ    0                   ; successful operation
    SYS_exit            equ    60                  ; code for terminate
    
    ; -----
    ;  Define Data.
    
    intNum         dd     1498
    section        .bss
    strNum            resb    10
    
    ; *********************************************************
    
    section         .text 
    global     _start 
    _start:
    
    ; Convert an integer to an ASCII string.
    
    ; -----
    ;  Part A - Successive division
    
        mov     eax, dword [intNum]         ; get integer
        mov     rcx, 0                      ; digitCount = 0
        mov     ebx, 10                     ; set for dividing by 10
    
    divideLoop:
        mov    edx, 0
        div    ebx                          ; divide number by 10
        
        push   rdx                          ; push remainder
        inc    rcx                          ; increment digitCount
        
        cmp    eax, 0                       ; if (result > 0)
        jne    divideLoop                   ;   goto divideLoop
        
    ; -----
    ;   Part B - Convert remainders and store
    
        mov    rbx, strNum                   ; get addr of string
        mov    rdi, 0                        ; idx = 0
        
    popLoop:
        pop    rax                           ; pop intDigit
        
        add    a1, "0"                       ; char = int + "0"
        
        mov    byte [rbx+rdi], a1            ; string[idx] = char
        inc    rdi                           ; increment idx
        loop   popLoop                       ; if (digit Count > 0)
                                             ;   goto popLoop
        mov    byte [rbx + rdi], NULL        ; string[idx] = NULL
        
        ; -----
        ; Done, terminate program.
        
        last:
             mov    rax, SYS_exit            ; call code for exit
             mov    rdi, EXIT_SUCCESS        ; exit with success
             syscall
    

    There are many different valid implementations for this algorithm. The program should be assembled to address any typos or syntax errors.


    This page titled 10.3: Implement the Program is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Ed Jorgensen.

    • Was this article helpful?