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