Difference between revisions of "CSC231 Homework 2 Solutions 2014"
Line 358: | Line 358: | ||
=Problem 5= | =Problem 5= | ||
<source lang="asm"> | <source lang="asm"> | ||
+ | ;;; ----------------------------------------------------- | ||
+ | ;;; 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 | ||
</source> | </source> | ||
=Problem 6= | =Problem 6= | ||
<source lang="asm"> | <source lang="asm"> | ||
+ | ;;; ----------------------------------------------------- | ||
+ | ;;; 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 | ||
</source> | </source> | ||
− | + | ||
</onlydft> | </onlydft> |