Difference between revisions of "CSC231 Homework 2 Solutions 2014"
(Created page with "--~~~~ ---- <onlydft> =Problem 1= <source lang="asm"> ;;; ----------------------------------------------------- ;;; hw2_1.asm ;;; D. Thiebaut ;;; ;;; To assemble, link and ru...") |
|||
Line 177: | Line 177: | ||
=Problem 3= | =Problem 3= | ||
<source lang="asm"> | <source lang="asm"> | ||
+ | ;;; ----------------------------------------------------- | ||
+ | ;;; 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 | ||
+ | |||
+ | |||
</source> | </source> | ||
=Problem 4= | =Problem 4= |