hw3_ex1.asm

; Jade Yu Cheng
; ICS 312
; Assignment 3 Exercise 1
; Feb 23, 2009

; this is an assembly program that prompts the user to type a 5-character
; string. The program reads the first 5 characters of that string and prints
; two strings. The first string contains the 5 characters entered by the user,
; but in reverse order. The second string contains the 5 characters entered by
; the user, but whose ASCII code has been decremented by 32.

%include "asm_io.inc"

segment .data
        msg1    db      "Enter a 5-character string: ", 0
        msg2    db      "String#1: ", 0
        msg3    db      "String#2: ", 0

segment .bss
        input   resb    6               ; reserve space for user input string
        reverse resb    6               ; reserve space for the reversed string

segment .text
        global asm_main
asm_main:
        enter   0,0                     ; setup
        pusha                           ; setup

        ; prompt the user to type in 5 charactors and store them in "input"
        mov     eax, msg1               ; let eax point to msg1
        call    print_string            ; prints out msg1
        mov     ebx, input              ; let ebx point to the "input"
        call    read_char               ; get a charactor from the keyboard
        mov     [ebx], al               ; store the charactor into "input"
        inc     ebx                     ; move the pointer to the next position
        call    read_char               ; get a charactor from the keyboard
        mov     [ebx], al               ; store the charactor into "input"
        inc     ebx                     ; move the pointer to the next position
        call    read_char               ; get a charactor from the keyboard
        mov     [ebx], al               ; store the charactor into "input"
        inc     ebx                     ; move the pointer to the next position
        call    read_char               ; get a charactor from the keyboard
        mov     [ebx], al               ; store the charactor into "input"
        inc     ebx                     ; move the pointer to the next position
        call    read_char               ; get a charactor from the keyboard
        mov     [ebx], al               ; store the charactor into "input"
        inc     ebx                     ; move the pointer to the next position
        mov     byte [ebx], 0           ; NULL terminates the "input" string

        ; read the charactors from "input", store them in "reverse" string in a
        ; reversed order, and print out the "reverse" string
        mov     eax, input              ; let eax point to "input"
        mov     ebx, reverse            ; let ebx point to "reverse"
        add     ebx, 5                  ; move ebx pointer the end of "reverse"
        mov     byte [ebx], 0           ; NULL terminates the "reverse" string
        dec     ebx                     ; decrement ebx pointer
        mov     cl, [eax]               ; store the charactor in "input" to cl
        mov     [ebx], cl               ; write the charactor to "reverse"
        inc     eax                     ; increment eax pointer
        dec     ebx                     ; decrement ebx pointer
        mov     cl, [eax]               ; store the charactor in "input" to cl
        mov     [ebx], cl               ; write the charactor to "reverse"
        inc     eax                     ; increment eax pointer
        dec     ebx                     ; decrement ebx pointer
        mov     cl, [eax]               ; store the charactor in "input" to cl
        mov     [ebx], cl               ; write the charactor to "reverse"
        inc     eax                     ; increment eax pointer
        dec     ebx                     ; decrement ebx pointer
        mov     cl, [eax]               ; store the charactor in "input" to cl
        mov     [ebx], cl               ; write the charactor to "reverse"
        inc     eax                     ; increment eax pointer
        dec     ebx                     ; decrement ebx pointer
        mov     cl, [eax]               ; store the charactor in "input" to cl
        mov     [ebx], cl               ; write the charactor to "reverse"
        mov     eax, msg2               ; let eax point to msg2
        call    print_string            ; print out msg2
        mov     eax, reverse            ; let eax point to "reverse"
        call    print_string            ; print out "reverse"
        call    print_nl                ; print a blank line

        ; read the charactors from "input", decrement the number by 32, and
        ; store them back to "input" string.
        mov     ebx, input              ; let ebx point to "input"
        mov     al, [ebx]               ; store the charactor in "input" to al
        sub     al, 32                  ; decrement al by 32
        mov     [ebx], al               ; write the modified charactor to ebx
        inc     ebx                     ; increment the ebx pointer
        mov     al, [ebx]               ; store the charactor in "input" to al
        sub     al, 32                  ; decrement al by 32
        mov     [ebx], al               ; store the charactor in "input" to al
        inc     ebx                     ; increment the ebx pointer
        mov     al, [ebx]               ; store the charactor in "input" to al
        sub     al, 32                  ; decrement al by 32
        mov     [ebx], al               ; store the charactor in "input" to al
        inc     ebx                     ; increment the ebx pointer
        mov     al, [ebx]               ; store the charactor in "input" to al
        sub     al, 32                  ; decrement al by 32
        mov     [ebx], al               ; store the charactor in "input" to al
        inc     ebx                     ; increment the ebx pointer
        mov     al, [ebx]               ; store the charactor in "input" to al
        sub     al, 32                  ; decrement al by 32
        mov     [ebx], al               ; store the charactor in "input" to al
        mov     eax, msg3               ; let eax point to msg3
        call    print_string            ; print out msg3
        mov     eax, input              ; let eax point to "input"
        call    print_string            ; print out the modified "input" string
        call    print_nl                ; print a blank line

        popa                            ; cleanup
        mov     eax, 0                  ; cleanup
        leave                           ; cleanup
        ret                             ; cleanup

Valid HTML 4.01 Valid CSS