Difference between revisions of "CSC231 Homework 7 Solution 2010"

From dftwiki3
Jump to: navigation, search
(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
 
;;;  
 
;;;  
;;; RB Axtell
+
;;; Program that takes an unsigned, 32-bit integer and prints it out.
;;; 231a-af
+
;;; Assumes integer (held in x) is in decimal form.
;;; 10/11/2010
+
;;; Uses the stack and at least one function.
 +
;;; Sample output:
 +
;;; 1234589
 
;;;
 
;;;
;;; Prints the last N digits of the integer x (uses leading
+
;;; to assemble and run:
;;; 0's if shorter than N)
 
 
;;;
 
;;;
;;;
+
;;;     nasm -f elf -F  stabs hw7.asm
;;; I had a function sepAndPush that broke off one integer
+
;;;     ld -melf_i386 -o hw7 hw7.o
;;; at a time and pushed it to the stack, but had problems
+
;;;     ./hw7
;;; because data was being pushed onto the stack after the
+
;;; -------------------------------------------------------------------
;;; return address. So it tried to use the last doubleword
+
;;; integer from x pushed to the stack as the ret address.
+
;%include files here...
;;;  
+
;;; To compile and run:
+
EXIT    equ    1
;;; nasm -f elf -F stabs hw7.asm
+
WRITE  equ    4
;;; ld -melf_i386 -o hw7 hw7.o
+
STDOUT  equ    1
;;; ./hw7
+
;;; --------------------------------------------------------
+
      ;; ------------------------------------------------------------
 +
;; 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
  
EXIT equ 1
+
call printNL
READ equ 3
 
WRITE equ 4
 
STDOUT equ 1
 
  
;; ------------------------------------------------
+
;;; call printNL
;; data area
 
;; ------------------------------------------------
 
  
section .data
 
  
x dd 1234589 ; number to print
+
N dd 10 ; length of number to print
+
;; exit()
tmp dd 0 ; to store the current int
+
theEnd:
 +
mov eax,EXIT
 +
mov ebx,0
 +
int 0x80 ; final system call
  
;; ------------------------------------------------
 
;; code area
 
;; ------------------------------------------------
 
  
section .text
 
global _start
 
  
_start:
+
 
nop ;no operation for debugging
+
 
nop
+
nop
+
 
mov ecx, dword[N] ;number of digits to print
+
;;; Function to put the decimal values of the integer in x onto the stack
mov eax, dword[x] ;the number to print
+
;;; 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
 +
  
break: xor edx, edx
 
div ebx ;divide the number by 10
 
push edx ;the remainder contains the last digit
 
loop break ;end break
 
  
mov ecx, [N] ;reset the counter
 
 
 
print: mov esi, ecx ;store the counter
+
pop ecx ;pop the next digit
+
;;; Funtion to put the ascii value of a given integer into temp
call printChar
+
;;; Assumes the location of the integer is on the stack
mov ecx, esi ;re-store the counter
+
moveTemp:
loop print ;end print
+
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
 +
 
  
;; Print a line feed
+
 
mov dword[tmp], 0x0a;line feed
+
 
 +
 +
;;; 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, tmp
+
mov ecx, temp
mov edx, 1
+
mov edx, 4
 
int 0x80
 
int 0x80
+
popad
jmp theEnd
+
ret
 +
 
 +
 
 +
 
  
;;; ------------ printChar -------------
 
;;; digit to print in ecx
 
;;;
 
;;; converts the number in ecx to ASCII
 
;;; and prints.
 
;;; ------------------------------------
 
 
 
printChar:
+
;;; Function to print a new line, to look nice
+
;;; Assumes nothing on the stack
mov dword[tmp], ecx ;tmp < digit to print
+
printNL:
add dword[tmp], 0x30;+0x30 = change to ASCII
+
pushad
 
mov eax, WRITE
 
mov eax, WRITE
 
mov ebx, STDOUT
 
mov ebx, STDOUT
mov ecx, tmp ;address of digit in memory
+
mov ecx, nl
mov edx, 1
+
mov edx, 4
 
int 0x80
 
int 0x80
 +
popad
 +
ret
 
 
ret ;end printChar
 
  
;; exit()
 
  
theEnd: mov eax, EXIT
 
mov ebx, 0
 
int 0x80 ; final system call
 
  
  

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