CSC231 Homework 2 Solutions 2014

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 13:41, 22 September 2014 (EDT)


Problem 1

;;; -----------------------------------------------------
;;; hw2_1.asm
;;; D. Thiebaut
;;;
;;; To assemble, link and run:
;;; nasm -f elf 231Lib.asm
;;; nasm -f elf hw2_1.asm
;;; ld -melf_i386 hw2_1.o 231Lib.o -o hw2_1
;;; ./hw2_1
;;; -----------------------------------------------------

;;; extern functions that will be linked to this program
;;; contained in 231Lib.asm

extern  _printDec
extern  _printString
extern  _println
extern  _getInput
        
;;; -----------------------------------------------------
;;; data section
;;; -----------------------------------------------------
        section .data
x       dd      0               ; the integer used for IO
msgX    db      "x = "
z       dd      0               ; the integer used for IO
msgZ    db      "z = "
prompt  db      "> "
        
;;; -----------------------------------------------------
;;; code section
;;; -----------------------------------------------------
        section .text
        global _start
_start:

;;; get number from user
        mov             ecx, prompt
        mov             edx, 2
        call            _printString
        call            _getInput
        mov             dword[x], eax

        
;;; print "x = dddd" where dddd is the contents of x
;;; in decimal
        mov             ecx, msgX
        mov             edx, 4
        call            _printString
        mov             eax, dword[x]
        call            _printDec
        call            _println

        mov             eax, dword[x]
        add             eax, eax
        add             eax, eax
        sub             eax, 1
        mov             dword[z], eax
        
        mov             ecx, msgZ
        mov             edx, 4
        call            _printString
        mov             eax, dword[z]
        call            _printDec
        call            _println
        
;;; ; exit
        mov     ebx, 0
        mov     eax, 1
        int     0x80

Problem 2

;;; -----------------------------------------------------
;;; hw2_2.asm
;;; D. Thiebaut
;;; Simple skeleton program illustrating how to get an
;;; integer from the command line and displaying it back
;;;
;;; To assemble, link and run:
;;; nasm -f elf 231Lib.asm
;;; nasm -f elf hw2_2.asm
;;; ld -melf_i386 hw2_2.o 231Lib.o -o hw2_2
;;; ./hw2_2
;;; -----------------------------------------------------

;;; extern functions that will be linked to this program
;;; contained in 231Lib.asm

extern  _printDec
extern  _printString
extern  _println
extern  _getInput
        
;;; -----------------------------------------------------
;;; data section
;;; -----------------------------------------------------
        section .data
x       dd      0               ; the integer used for IO
msgX    db      "x = "
z       dd      0               ; the integer used for IO
msgZ    db      "z = "
y       dd      0               ; the integer used for IO
msgY    db      "y = "
prompt  db      "> "
        
;;; -----------------------------------------------------
;;; code section
;;; -----------------------------------------------------
        section .text
        global _start
_start:

;;; get number from user
        mov             ecx, prompt
        mov             edx, 2
        call            _printString
        call            _getInput
        mov             dword[x], eax

;;; get number from user
        mov             ecx, prompt
        mov             edx, 2
        call            _printString
        call            _getInput
        mov             dword[y], eax

        
;;; print "x = dddd" where dddd is the contents of x
;;; in decimal
        mov             ecx, msgX
        mov             edx, 4
        call            _printString
        mov             eax, dword[x]
        call            _printDec
        call            _println

;;; print "y = dddd" where dddd is the contents of x
;;; in decimal
        mov             ecx, msgY
        mov             edx, 4
        call            _printString
        mov             eax, dword[y]
        call            _printDec
        call            _println

;;; compute z = 2 * x - 3 * y + 7
        mov             eax, dword[x]
        add             eax, eax
        sub             eax, dword[y]
        sub             eax, dword[y]
        sub             eax, dword[y]
        add             eax, 7
        mov             dword[z], eax
        
        mov             ecx, msgZ
        mov             edx, 4
        call            _printString
        mov             eax, dword[z]
        call            _printDec
        call            _println
        
;;; ; exit
        mov     ebx, 0
        mov     eax, 1
        int     0x80

Problem 3

 ;;; -----------------------------------------------------
;;; hw2_3.asm
;;; D. Thiebaut
;;; Simple skeleton program illustrating how to get an
;;; integer from the command line and displaying it back
;;;
;;; To assemble, link and run:
;;; nasm -f elf 231Lib.asm
;;; nasm -f elf hw2_3.asm
;;; ld -melf_i386 hw2_3.o 231Lib.o -o hw2_3
;;; ./hw2_3
;;; -----------------------------------------------------

;;; extern functions that will be linked to this program
;;; contained in 231Lib.asm

extern  _printDec
extern  _printString
extern  _println
extern  _getInput
        
;;; -----------------------------------------------------
;;; data section
;;; -----------------------------------------------------
        section .data
x       dd      0               ; the integer used for IO
msgX    db      "x = "
z       dd      0               ; the integer used for IO
msgZ    db      "z = "
y       dd      0               ; the integer used for IO
msgY    db      "y = "
prompt  db      "> "
        
;;; -----------------------------------------------------
;;; code section
;;; -----------------------------------------------------
        section .text
        global _start
_start:

;;; get number from user
        mov             ecx, prompt
        mov             edx, 2
        call            _printString
        call            _getInput
        mov             dword[x], eax

;;; get number from user
        mov             ecx, prompt
        mov             edx, 2
        call            _printString
        call            _getInput
        mov             dword[y], eax

        
;;; print "x = dddd" where dddd is the contents of x
;;; in decimal
        mov             ecx, msgX
        mov             edx, 4
        call            _printString
        mov             eax, dword[x]
        call            _printDec
        call            _println

;;; print "y = dddd" where dddd is the contents of x
;;; in decimal
        mov             ecx, msgY
        mov             edx, 4
        call            _printString
        mov             eax, dword[y]
        call            _printDec
        call            _println

;;; compute z = ( x + y )* 3 + ( x - y ) * 4
        
        mov             eax, dword[x]
        add             eax, dword[y]
        mov             ebx, eax        ; x + y
        add             ebx, eax        ; 2*(x+y)
        add             ebx, eax        ; 3*(x+y)

        mov             eax, dword[x]
        sub             eax, dword[y]   ; (x-y)
        add             eax, eax        ; 2*(x-y)
        add             eax, eax        ; 4*(x-y)

        sub             ebx, eax        ; 3(x+y) - 4(x-y)
        mov             dword[z], ebx
        
        mov             ecx, msgZ       
        mov             edx, 4
        call            _printString
        mov             eax, dword[z]
        call            _printDec
        call            _println
        
;;; ; exit
        mov     ebx, 0
        mov     eax, 1
        int     0x80

Problem 4

;;; -----------------------------------------------------
;;; hw2_4.asm
;;; D. Thiebaut
;;;
;;; To assemble, link and run:
;;; nasm -f elf 231Lib.asm
;;; nasm -f elf hw2_4.asm
;;; ld -melf_i386 hw2_4.o 231Lib.o -o hw2_4
;;; ./hw2_4
;;; -----------------------------------------------------

;;; extern functions that will be linked to this program
;;; contained in 231Lib.asm

extern  _printDec
extern  _printString
extern  _println
extern  _getInput
        
;;; -----------------------------------------------------
;;; data section
;;; -----------------------------------------------------
        section .data
x       dw      0               ; the integer used for IO
msgX    db      "x = "
z       dw      0               ; the integer used for IO
msgZ    db      "z = "
prompt  db      "> "
        
;;; -----------------------------------------------------
;;; code section
;;; -----------------------------------------------------
        section .text
        global _start
_start:

;;; get number from user
        mov             ecx, prompt
        mov             edx, 2
        call            _printString
        call            _getInput
        mov             word[x], ax

        
;;; print "x = dddd" where dddd is the contents of x
;;; in decimal
        mov             ecx, msgX
        mov             edx, 4
        call            _printString
        mov             eax, 0
        mov             ax, word[x]
        call            _printDec
        call            _println

        mov             eax, 0
        mov             ax, word[x]
        add             ax, ax
        add             ax, ax
        sub             ax, 1
        mov             word[z], ax
        
        mov             ecx, msgZ
        mov             edx, 4
        call            _printString
        mov             eax, 0
        mov             ax, word[z]
        call            _printDec
        call            _println
        
;;; ; exit
        mov     ebx, 0
        mov     eax, 1
        int     0x80

Problem 5

;;; -----------------------------------------------------
;;; Hw2_5.asm
;;; D. Thiebaut
;;; Simple skeleton program illustrating how to get an
;;; integer from the command line and displaying it back
;;;
;;; To assemble, link and run:
;;; nasm -f elf 231Lib.asm
;;; nasm -f elf Hw2_5.asm
;;; ld -melf_i386 Hw2_5.o 231Lib.o -o Hw2_5
;;; ./Hw2_5
;;; -----------------------------------------------------

;;; extern functions that will be linked to this program
;;; contained in 231Lib.asm

extern  _printDec
extern  _printString
extern  _println
extern  _getInput
        
;;; -----------------------------------------------------
;;; data section
;;; -----------------------------------------------------
        section .data
x       dw      0               ; the integer used for IO
msgX    db      "x = "
z       dw      0               ; the integer used for IO
msgZ    db      "z = "
y       dw      0               ; the integer used for IO
msgY    db      "y = "
prompt  db      "> "
        
;;; -----------------------------------------------------
;;; code section
;;; -----------------------------------------------------
        section .text
        global _start
_start:

;;; get number from user
        mov             ecx, prompt
        mov             edx, 2
        call            _printString
        call            _getInput
        mov             word[x], ax

;;; get number from user
        mov             ecx, prompt
        mov             edx, 2
        call            _printString
        call            _getInput
        mov             word[y], ax

        
;;; print "x = dddd" where dddd is the contents of x
;;; in decimal
        mov             ecx, msgX
        mov             edx, 4
        call            _printString
        mov             eax, 0
        mov             ax, word[x]
        call            _printDec
        call            _println

;;; print "y = dddd" where dddd is the contents of x
;;; in decimal
        mov             ecx, msgY
        mov             edx, 4
        call            _printString
        mov             eax, 0
        mov             ax, word[y]
        call            _printDec
        call            _println

;;; compute z = 2 * x - 3 * y + 7
        mov             ax, word[x]
        add             ax, ax
        sub             ax, word[y]
        sub             ax, word[y]
        sub             ax, word[y]
        add             ax, 7
        mov             word[z], ax
        
        mov             ecx, msgZ
        mov             edx, 4
        call            _printString
        mov             eax, 0
        mov             ax, word[z]
        call            _printDec
        call            _println
        
;;; ; exit
        mov     ebx, 0
        mov     eax, 1
        int     0x80

Problem 6

;;; -----------------------------------------------------
;;; Hw2_6.asm
;;; D. Thiebaut
;;; Simple skeleton program illustrating how to get an
;;; integer from the command line and displaying it back
;;;
;;; To assemble, link and run:
;;; nasm -f elf 231Lib.asm
;;; nasm -f elf Hw2_6.asm
;;; ld -melf_i386 Hw2_6.o 231Lib.o -o Hw2_6
;;; ./Hw2_6
;;; -----------------------------------------------------

;;; extern functions that will be linked to this program
;;; contained in 231Lib.asm

extern  _printDec
extern  _printString
extern  _println
extern  _getInput
        
;;; -----------------------------------------------------
;;; data section
;;; -----------------------------------------------------
        section .data
x       db      0               ; the integer used for IO
msgX    db      "x = "
z       db      0               ; the integer used for IO
msgZ    db      "z = "
y       db      0               ; the integer used for IO
msgY    db      "y = "
prompt  db      "> "
        
;;; -----------------------------------------------------
;;; code section
;;; -----------------------------------------------------
        section .text
        global _start
_start:

;;; get number from user
        mov             ecx, prompt
        mov             edx, 2
        call            _printString
        call            _getInput
        mov             byte[x], al

;;; get number from user
        mov             ecx, prompt
        mov             edx, 2
        call            _printString
        call            _getInput
        mov             byte[y], al

        
;;; print "x = dddd" where dddd is the contents of x
;;; in decimal
        mov             ecx, msgX
        mov             edx, 4
        call            _printString
        mov             eax, 0
        mov             al, byte[x]
        call            _printDec
        call            _println

;;; print "y = dddd" where dddd is the contents of x
;;; in decimal
        mov             ecx, msgY
        mov             edx, 4
        call            _printString
        mov             eax, 0
        mov             al, byte[y]
        call            _printDec
        call            _println

;;; compute z = ( x + y )* 3 + ( x - y ) * 4
        
        mov             al, byte[x]
        add             al, byte[y]
        mov             bl, al          ; x + y
        add             bl, al          ; 2*(x+y)
        add             bl, al          ; 3*(x+y)

        mov             al, byte[x]
        sub             al, byte[y]     ; (x-y)
        add             al, al          ; 2*(x-y)
        add             al, al          ; 4*(x-y)

        sub             bl, al          ; 3(x+y) - 4(x-y)
        mov             byte[z], bl
        
        mov             ecx, msgZ       
        mov             edx, 4
        call            _printString
        mov             eax, 0
        mov             al, byte[z]
        call            _printDec
        call            _println
        
;;; ; exit
        mov     ebx, 0
        mov     eax, 1
        int     0x80