Difference between revisions of "CSC231 Homework 7 2010"

From dftwiki3
Jump to: navigation, search
(Requirements)
(Misc. information)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] 01:21, 5 November 2010 (UTC)
 
--[[User:Thiebaut|D. Thiebaut]] 01:21, 5 November 2010 (UTC)
 
----
 
----
<center>
+
 
<font size="+2">Page under construction!</font>
 
<br \>[[File:UnderConstruction.jpg|300px]]
 
</center>
 
  
 
__TOC__
 
__TOC__
Line 159: Line 156:
 
     """ pops item from the stack and returns it"""
 
     """ pops item from the stack and returns it"""
 
     global stack
 
     global stack
     digit = stack[-1]
+
     return stack.pop()
    stack = stack[:-1]
 
    return digit
 
  
 
def stackIsEmpty():
 
def stackIsEmpty():
Line 196: Line 191:
  
 
;Note 2
 
;Note 2
: The Python program above uses a while loop that stops when the stack is empty.  Since we do not know how to test yet, simply use for-loops that will assume that each integer can be printed with ''N'' digits, where you have to figure out ''N''.  
+
: The Python program above uses a while loop that stops when the stack is empty.  Since we do not know how to test yet, simply use for-loops that will assume that each integer can be printed with ''N'' digits, where you have to figure out ''N''. <br />So 1234 would be printed as 0...001234.  Leading zeros are fine in this assignment.
So 1234 would be printed as 0...001234.  Leading zeros are fine in this assignment.
+
 
 +
;Note 3
 +
: You can assume that all the integers we are interested in printing out are '''unsigned''' 32-bit integers.
 +
 
 +
;Note 4
 +
: Be careful how you use functions as they may interfere with your use of the stack for keeping digits in...
 +
 
 +
;Note 5
 +
: if you have the integer number 5 in al, and if you want to print it as "5", remember that you have to first transform 5 into 0x35, which is the ASCII code for "5", and then print it!
 +
 
 +
 
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC231]][[Category:Homework]]

Latest revision as of 18:45, 21 November 2010

--D. Thiebaut 01:21, 5 November 2010 (UTC)



This assignment is due on 11/10/10 (another binary date), at 11:59 p.m. + 1 minute. You can work on this assignment in groups of two if you wish.

Running out of stack...

Given the assembly language program shown below:

;;; hw7.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:
;;;
;;; nasm -f elf -F stabs hw7.asm
;;; ld hw7.o -o hw7
;;; ./hw7
;;;  

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 hw7 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 hw7.txt and submit it as follows:


      submit hw7 hw7.txt

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

Problem 2: Printing an integer in decimal

To print the contents of a double-word in decimal is no trivial task. Your assignment is to do just that: define a double word equal to some quantity, and make your program print its decimal equivalent on the screen.

Requirements

  • You cannot use the driver.c wrapper.
  • You must use int 0x80 interrupts to print each digit of the number as a character.
  • You must use the stack and its push and pop instructions.
  • You must use at least one function.

Misc. information

  • To make grading easier, please store the value 1234589 in your double word before you print it.
         section  .data
  x      dd       1234589
  • The output of your program should simply be:


  1234589
  • Store your program in a file called hw7.asm and submit it the same way you submitted hw7.txt.
  • Use an approach similar to the one illustrated in the python program below. Note that in assembly we can directly push and pop registers or variables in the stack with the push and pop instructions, while in python we have to create special functions to deal with a stack data-structure.
# printdec.py
# D. Thiebaut
#

x = 1234589
stack = []

def push( x ):
    """ pushes item x in the stack """
    global stack
    stack.append( x )

def pop( ):
    """ pops item from the stack and returns it"""
    global stack
    return stack.pop()

def stackIsEmpty():
    """ returns true if stack is empty"""
    global stack
    return len( stack )==0


def main():
    """ takes x and decomposes it into digits that are entered in a stack """
    """ then pops all the digits from the stack and prints them on the screen"""
    global x

    #--- break one digit off x at a time, ane push it in stack ---
    while True:
        digit = x % 10
        push( digit )
        x = x / 10
        if x == 0:
            break

    #--- pop digits off top of stack and print them ---
    while not stackIsEmpty():
        x = pop()
        print x,
        
    print
    
main()    
Note 1
The push and pop instructions allow you to work with words and double-words, not with bytes. So, if you want/need to save the contents of al on the stack, you cannot do push al. This instruction is not supported. Instead, do push ax, which will push both al and ah. So, when you want to get al back from the stack, simply issue pop ax, and you will get al (and ah) restored. Note that depending on how you organize your code, you may not have to worry about pushing/popping byte quantities...
Note 2
The Python program above uses a while loop that stops when the stack is empty. Since we do not know how to test yet, simply use for-loops that will assume that each integer can be printed with N digits, where you have to figure out N.
So 1234 would be printed as 0...001234. Leading zeros are fine in this assignment.
Note 3
You can assume that all the integers we are interested in printing out are unsigned 32-bit integers.
Note 4
Be careful how you use functions as they may interfere with your use of the stack for keeping digits in...
Note 5
if you have the integer number 5 in al, and if you want to print it as "5", remember that you have to first transform 5 into 0x35, which is the ASCII code for "5", and then print it!