CSC231 Homework 1 Solution 2014

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 07:55, 9 October 2014 (EDT)


Problem 2


;;; ; hw1_2.asm
;;; ; D. Thiebaut
;;; ;
;;; ; Displays a triangle of As and Bs:
;;; ; A
;;; ; AA
;;; ; AAA
;;; ; AAAA
;;; ; AAAAA
;;; ; BBBBB
;;; ; BBBB
;;; ; BBB
;;; ; BB
;;; ; B
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ;     nasm -f elf -F  stabs hw1_2.asm
;;; ;     nasm -f elf -F stabs 231Lib.asm
;;; ;     ld -melf_i3896 -o hw1_2 hw1_2.o 231Lib.o
;;; ;     ./hw1_2
;;; ; -------------------------------------------------------------------

;;;  ------------------------------------------------------------
;;;  data areas
;;;  ------------------------------------------------------------

                section .data
msg             db      "AAAAA", 10

        
;;;  ------------------------------------------------------------
;;;  code area
;;;  ------------------------------------------------------------
                section .text
                global  _start

_start:
                mov     ecx, msg+4      ;print A
                mov     edx, 2
                call    printStr

                mov     ecx, msg+3      ;print AA
                mov     edx, 3
                call    printStr
        
                mov     ecx, msg+2      ;print AAA
                mov     edx, 4
                call    printStr

                mov     ecx, msg+1      ;print AAAA
                mov     edx, 5
                call    printStr

                mov     ecx, msg        ;print AAAAA
                mov     edx, 6
                call    printStr

                inc     byte [msg]      ;msg now contains BAAAA
                inc     byte [msg+1]    ;msg now contains BBAAA
                inc     byte [msg+2]    ;msg now contains BBBAA
                inc     byte [msg+3]    ;msg now contains BBBBA
                inc     byte [msg+4]    ;msg now contains BBBBB
        
                mov     ecx, msg        ;print BBBBB
                mov     edx, 6
                call    printStr

                mov     ecx, msg+1      ;print BBBB
                mov     edx, 5
                call    printStr
        
                mov     ecx, msg+2      ;print BBB
                mov     edx, 4
                call    printStr

                mov     ecx, msg+3      ;print BB
                mov     edx, 3
                call    printStr

                mov     ecx, msg+4      ;print B
                mov     edx, 2
                call    printStr



;;;  exit()

                mov     eax,1
                mov     ebx,0
                int     0x80    ; final system call

;;; ----------------------------------------------------------
;;; Function: print string
;;; gets address of string in ecx
;;; gets length of string in edx
;;; MODIFIED REGISTERS: eax, ebx
printStr:       mov     eax, 4
                mov     ebx, 1
                int     0x80
                ret