Difference between revisions of "CSC231 Homework 7 Solution 2010"
(→Part 1) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] 15:04, 19 November 2010 (UTC) | --[[User:Thiebaut|D. Thiebaut]] 15:04, 19 November 2010 (UTC) | ||
---- | ---- | ||
+ | <onlydft> | ||
=Part 1= | =Part 1= | ||
<code><pre> | <code><pre> | ||
Line 25: | Line 26: | ||
</pre></code> | </pre></code> | ||
+ | </onlydft> | ||
=Part 2= | =Part 2= | ||
<code><pre> | <code><pre> | ||
− | |||
;;; hw7.asm | ;;; hw7.asm | ||
+ | ;;; Amy Tayloe, 231a-ai | ||
+ | ;;; Last modified 11/10/10 | ||
;;; | ;;; | ||
− | ;;; | + | ;;; Program that takes an unsigned, 32-bit integer and prints it out. |
− | ;;; | + | ;;; Assumes integer (held in x) is in decimal form. |
− | ;;; | + | ;;; Uses the stack and at least one function. |
+ | ;;; Sample output: | ||
+ | ;;; 1234589 | ||
;;; | ;;; | ||
− | ;;; | + | ;;; to assemble and run: |
− | |||
;;; | ;;; | ||
− | ;;; | + | ;;; nasm -f elf -F stabs hw7.asm |
− | ;;; | + | ;;; ld -melf_i386 -o hw7 hw7.o |
− | ;;; | + | ;;; ./hw7 |
− | ;;; | + | ;;; ------------------------------------------------------------------- |
− | ;;; | + | |
− | ;;; | + | ;%include files here... |
− | ;;; | + | |
− | ;;; | + | EXIT equ 1 |
− | ;;; | + | WRITE equ 4 |
− | ;; | + | STDOUT equ 1 |
− | ;; | + | |
− | ;; | + | ;; ------------------------------------------------------------ |
+ | ;; Data Area | ||
+ | ;; ------------------------------------------------------------ | ||
+ | section .data | ||
+ | ;;; Variable to print | ||
+ | x dd 1234589 | ||
+ | ;;; Array to hold the decimal values of x | ||
+ | arrx dd 0,0,0,0,0,0,0,0,0,0 | ||
+ | ;;; Place in memory to hold ascii value of integer to be printed | ||
+ | temp dd 0 | ||
+ | nl dd 10 | ||
+ | |||
+ | ;; ------------------------------------------------------------ | ||
+ | ;; Code Area | ||
+ | ;; ------------------------------------------------------------ | ||
+ | |||
+ | section .text | ||
+ | global _start | ||
+ | |||
+ | _start: | ||
+ | |||
+ | ;;; Master function | ||
+ | call getDecimal | ||
+ | mov eax, arrx | ||
+ | ;;; Loops N times, where N is the number of integers | ||
+ | mov ecx, 10 | ||
+ | .for: | ||
+ | push eax | ||
+ | call moveTemp | ||
+ | call printTemp | ||
+ | ;;; Increments eax by 4 to go to the next value | ||
+ | add eax, 4 | ||
+ | loop .for | ||
− | + | call printNL | |
− | |||
− | |||
− | |||
− | + | ;;; call printNL | |
− | |||
− | |||
− | |||
− | + | ||
− | + | ;; exit() | |
− | + | theEnd: | |
+ | mov eax,EXIT | ||
+ | mov ebx,0 | ||
+ | int 0x80 ; final system call | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | mov ecx, | + | ;;; Function to put the decimal values of the integer in x onto the stack |
− | mov eax, | + | ;;; Assumes nothing on the stack |
+ | getDecimal: | ||
+ | pushad | ||
+ | |||
+ | ;;; Loop 10 times (held in nl) | ||
+ | mov ecx, [nl] | ||
+ | dec ecx | ||
+ | .for: | ||
+ | ;;; Put the integer into eax, to be divided | ||
+ | mov edx, 0 | ||
+ | mov eax, [x] | ||
mov ebx, 10 | mov ebx, 10 | ||
+ | div ebx | ||
+ | ;;; Replace previous value with quotient | ||
+ | mov [x], eax | ||
+ | ;;; Push the remainder into the array | ||
+ | mov ebx, arrx | ||
+ | add ebx, ecx | ||
+ | add ebx, ecx | ||
+ | add ebx, ecx | ||
+ | add ebx, ecx | ||
+ | mov [ebx], edx | ||
+ | loop .for | ||
+ | popad | ||
+ | ret | ||
+ | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | ||
− | + | ;;; Funtion to put the ascii value of a given integer into temp | |
− | + | ;;; Assumes the location of the integer is on the stack | |
− | mov | + | moveTemp: |
− | + | push ebp | |
+ | mov ebp, esp | ||
+ | pushad | ||
+ | ;;; Move the location of the integer into ebx | ||
+ | mov ebx, [ebp+8] | ||
+ | ;;; Move the integer into eax | ||
+ | mov eax, [ebx] | ||
+ | ;;; Increments the value by 48 (the ascii decimal value of 0) | ||
+ | add eax, 48 | ||
+ | ;;; Put that value into temp | ||
+ | mov [temp], eax | ||
+ | |||
+ | popad | ||
+ | pop ebp | ||
+ | ret 1*4 | ||
+ | |||
− | ;; | + | |
− | + | ||
+ | |||
+ | ;;; Function to print the word held at temp | ||
+ | ;;; Assumes nothing on the stack | ||
+ | printTemp: | ||
+ | pushad | ||
mov eax, WRITE | mov eax, WRITE | ||
mov ebx, STDOUT | mov ebx, STDOUT | ||
− | mov ecx, | + | mov ecx, temp |
− | mov edx, | + | mov edx, 4 |
int 0x80 | int 0x80 | ||
− | + | popad | |
− | + | ret | |
+ | |||
+ | |||
+ | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | ;;; Function to print a new line, to look nice | |
− | + | ;;; Assumes nothing on the stack | |
− | + | printNL: | |
− | + | pushad | |
mov eax, WRITE | mov eax, WRITE | ||
mov ebx, STDOUT | mov ebx, STDOUT | ||
− | mov ecx, | + | mov ecx, nl |
− | mov edx, | + | mov edx, 4 |
int 0x80 | int 0x80 | ||
+ | popad | ||
+ | ret | ||
− | |||
− | |||
− | |||
− | |||
− | |||
Latest revision as of 14:45, 7 November 2012
--D. Thiebaut 15:04, 19 November 2010 (UTC)
Part 2
;;; hw7.asm
;;; Amy Tayloe, 231a-ai
;;; Last modified 11/10/10
;;;
;;; Program that takes an unsigned, 32-bit integer and prints it out.
;;; Assumes integer (held in x) is in decimal form.
;;; Uses the stack and at least one function.
;;; Sample output:
;;; 1234589
;;;
;;; to assemble and run:
;;;
;;; nasm -f elf -F stabs hw7.asm
;;; ld -melf_i386 -o hw7 hw7.o
;;; ./hw7
;;; -------------------------------------------------------------------
;%include files here...
EXIT equ 1
WRITE equ 4
STDOUT equ 1
;; ------------------------------------------------------------
;; Data Area
;; ------------------------------------------------------------
section .data
;;; Variable to print
x dd 1234589
;;; Array to hold the decimal values of x
arrx dd 0,0,0,0,0,0,0,0,0,0
;;; Place in memory to hold ascii value of integer to be printed
temp dd 0
nl dd 10
;; ------------------------------------------------------------
;; Code Area
;; ------------------------------------------------------------
section .text
global _start
_start:
;;; Master function
call getDecimal
mov eax, arrx
;;; Loops N times, where N is the number of integers
mov ecx, 10
.for:
push eax
call moveTemp
call printTemp
;;; Increments eax by 4 to go to the next value
add eax, 4
loop .for
call printNL
;;; call printNL
;; exit()
theEnd:
mov eax,EXIT
mov ebx,0
int 0x80 ; final system call
;;; Function to put the decimal values of the integer in x onto the stack
;;; Assumes nothing on the stack
getDecimal:
pushad
;;; Loop 10 times (held in nl)
mov ecx, [nl]
dec ecx
.for:
;;; Put the integer into eax, to be divided
mov edx, 0
mov eax, [x]
mov ebx, 10
div ebx
;;; Replace previous value with quotient
mov [x], eax
;;; Push the remainder into the array
mov ebx, arrx
add ebx, ecx
add ebx, ecx
add ebx, ecx
add ebx, ecx
mov [ebx], edx
loop .for
popad
ret
;;; Funtion to put the ascii value of a given integer into temp
;;; Assumes the location of the integer is on the stack
moveTemp:
push ebp
mov ebp, esp
pushad
;;; Move the location of the integer into ebx
mov ebx, [ebp+8]
;;; Move the integer into eax
mov eax, [ebx]
;;; Increments the value by 48 (the ascii decimal value of 0)
add eax, 48
;;; Put that value into temp
mov [temp], eax
popad
pop ebp
ret 1*4
;;; Function to print the word held at temp
;;; Assumes nothing on the stack
printTemp:
pushad
mov eax, WRITE
mov ebx, STDOUT
mov ecx, temp
mov edx, 4
int 0x80
popad
ret
;;; Function to print a new line, to look nice
;;; Assumes nothing on the stack
printNL:
pushad
mov eax, WRITE
mov ebx, STDOUT
mov ecx, nl
mov edx, 4
int 0x80
popad
ret