CSC231 Homework 9 2012

From dftwiki3
Revision as of 14:38, 7 November 2012 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- =Problem 1: Running out of stack...= Given the assembly language program shown below: ;;; hw9.asm ;;; D. Thiebaut ;;; displays a simple hello msg on the screen u...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 14:38, 7 November 2012 (EST)


Problem 1: Running out of stack...

Given the assembly language program shown below:

;;; hw9.asm
;;; D. Thiebaut
;;; displays a simple hello msg on the screen using int 0x80, 
;;  in a loop that goes around "many" times!
;;; To assemble, link and run:
;;;
;;; nasmld hw9
;;;  

EXIT    equ     1
READ    equ     3 
WRITE   equ      4
STDOUT  equ     1

        
       ;; -------------------------------------
       ;; data segment
       ;; -------------------------------------
       section .data
msg     db      "hello world!",0x0a 
MSGLEN  equ     $-msg
       
       ;; -------------------------------------
       ;; code segment
       ;; -------------------------------------
       section .text
       global  _start
       
_start: mov     ecx, 0          ; get ready to loop

for:    mov     eax,WRITE       ; print message 
       mov     ebx,STDOUT      ; to screen
       push    ecx        
       mov     ecx,msg         ; address of msg
       mov     edx,MSGLEN      ; # chars to print

       int     0x80            ; ask os to pring msg
       pop     ecx
       push    ecx
       loop    for

       ;; exit
       
       mov     eax,EXIT        ; return to OS
       mov     ebx,0
       int     0x80


You will notice that the loop uses ecx as a counter, and int 0x80 uses ecx as well to hold the address of the string to print. The programmer decided to use the stack to push and pop the contents of ecx representing the loop counter. Unfortunately, this programmer uses one too many push instructions. The instruction in red is a bug. The ecx register is pushed twiced in the loop, but popped only once. As a result the stack will grow by one double-word every time the loop goes through one round.

Assume that when the executable version of the hw9 program is loaded into memory, the code is stored first, then the data, then the space for the stack is reserved on top of the data.


             |              | high memory addresses
             +--------------+
             |              |<--- ESP
             |   stack      |
             |              |
             |              |
             |              |
             +--------------+
             |   data       |
             |              |
             +--------------+
             |              |
             |   code       |
             |              | 
             +--------------+ 
             |              | 
             |              | 
             |              | low memory addresses

Before letting the processor start to execute the first instruction of the program, the operating system will set the ESP register to point to the top of the stack.

Question 1
How many lines "hello world" would the program print if it didn't have a bug in it?
Question 2
Assume that Linux gives every program a stack of a 1000 bytes when it is loaded into memory. What will happen to this buggy program when it is loaded and executed? Will it run to completion? Will it stop before completing? How will it stop? How many lines "hello world" will it print?
Question 3
Imagine that the output of the program is captured to paper. Hence we have a huge collection of "hello world" strings printed on paper. How will we notice the effect of the bug on paper? In other words, what will be printed that will show the effect of the bug?
Question 4
How many lines of "hello world" will the buggy program print? Although you may not get the exact number, try to be precise in evaluating this number of lines.

Submission

Save your answers in a file called hw9.txt and submit it as follows:


      submit hw9 hw9.txt

Make sure to include your name and account number at the top of the file.