CSC231 Homework 7 Solution 2010

From dftwiki3
Revision as of 10:08, 19 November 2010 by Thiebaut (talk | contribs) (Part 2)
Jump to: navigation, search

--D. Thiebaut 15:04, 19 November 2010 (UTC)


Part 1

hw7.txt

RB Axtell and 	Amy Tayloe  (Edited by D.T.)
231a-af		231a-ai
10/11/2010

1) It would print ecx lines. The code has ecx as 0 so it will loop 4 billion times. 
   0 decrements to -1 which in unsigned format is FFFF FFFF.  The value after that
   will be FFFF FFFE, and so on, down to 0.

2) Each time through the loop 4 bytes are added the stack. If the stack has 1000 bytes, 
   after 251 (1000/4 + 1) lines of "hello world" are printed a segmentation fault will be 
   thrown before the data section is overwritten. It will not run to completion (256 lines printed), 
   but will stop 5 lines short of the end.

3) If standard error is sent to the same paper, the words "Segmentation fault" will be printed 
    after 251 lines. Otherwise, there will be no evidence of the bug without counting the number 
    of lines.

4) A quarter of the stack plus 1 or 256 if the stack is big enough (1024 bytes).

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
	jmp	theEnd




	

;;; 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
	



	
	;; exit()
theEnd:
	mov	eax,EXIT
	mov	ebx,0
	int	0x80		; final system call