Difference between revisions of "CSC231 Homework 7 Solution 2010"

From dftwiki3
Jump to: navigation, search
(Created page with '--~~~~ ---- =Part 1= <code><pre> hw7.txt RB Axtell and Amy Tayloe 231a-af 231a-ai 10/11/2010 1) It would print ecx lines. The code has ecx as 0 so it will loop 256 times. 0 d…')
 
 
(3 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>
 
hw7.txt
 
hw7.txt
  
RB Axtell and Amy Tayloe
+
RB Axtell and Amy Tayloe (Edited by D.T.)
 
231a-af 231a-ai
 
231a-af 231a-ai
 
10/11/2010
 
10/11/2010
  
1) It would print ecx lines. The code has ecx as 0 so it will loop 256 times. 0 decrements to -1 which in signed numbers is 255.
+
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.
+
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.
+
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).
 
4) A quarter of the stack plus 1 or 256 if the stack is big enough (1024 bytes).
  
 
</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
 +
 
 +
call printNL
 +
 
 +
;;; call printNL
 +
 
  
EXIT equ 1
+
READ equ 3
+
;; exit()
WRITE equ 4
+
theEnd:
STDOUT equ 1
+
mov eax,EXIT
 +
mov ebx,0
 +
int 0x80 ; final system call
  
;; ------------------------------------------------
 
;; data area
 
;; ------------------------------------------------
 
  
section .data
 
  
x dd 1234589 ; number to print
 
N dd 10 ; length of number to print
 
tmp dd 0 ; to store the current int
 
  
;; ------------------------------------------------
 
;; code area
 
;; ------------------------------------------------
 
  
section .text
+
global _start
 
  
_start:
+
;;; Function to put the decimal values of the integer in x onto the stack
nop ;no operation for debugging
+
;;; Assumes nothing on the stack
nop
+
getDecimal:
nop
+
pushad
mov ecx, dword[N] ;number of digits to print
+
mov eax, dword[x] ;the number to print
+
;;; 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