Difference between revisions of "CSC231 Homework 7 Solution 2010"
(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…') |
(→Part 1) |
||
Line 5: | Line 5: | ||
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 | + | 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> | ||
+ | |||
=Part 2= | =Part 2= | ||
<code><pre> | <code><pre> |
Revision as of 10:06, 19 November 2010
--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
;;;
;;; RB Axtell
;;; 231a-af
;;; 10/11/2010
;;;
;;; Prints the last N digits of the integer x (uses leading
;;; 0's if shorter than N)
;;;
;;;
;;; I had a function sepAndPush that broke off one integer
;;; at a time and pushed it to the stack, but had problems
;;; 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.
;;;
;;; To compile and run:
;;; nasm -f elf -F stabs hw7.asm
;;; ld -melf_i386 -o hw7 hw7.o
;;; ./hw7
;;; --------------------------------------------------------
EXIT equ 1
READ equ 3
WRITE equ 4
STDOUT equ 1
;; ------------------------------------------------
;; 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:
nop ;no operation for debugging
nop
nop
mov ecx, dword[N] ;number of digits to print
mov eax, dword[x] ;the number to print
mov ebx, 10
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
call printChar
mov ecx, esi ;re-store the counter
loop print ;end print
;; Print a line feed
mov dword[tmp], 0x0a;line feed
mov eax, WRITE
mov ebx, STDOUT
mov ecx, tmp
mov edx, 1
int 0x80
jmp theEnd
;;; ------------ printChar -------------
;;; digit to print in ecx
;;;
;;; converts the number in ecx to ASCII
;;; and prints.
;;; ------------------------------------
printChar:
mov dword[tmp], ecx ;tmp < digit to print
add dword[tmp], 0x30;+0x30 = change to ASCII
mov eax, WRITE
mov ebx, STDOUT
mov ecx, tmp ;address of digit in memory
mov edx, 1
int 0x80
ret ;end printChar
;; exit()
theEnd: mov eax, EXIT
mov ebx, 0
int 0x80 ; final system call