pgubook-readers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pgubook-readers] memory and addressing


From: Peter Parkes
Subject: [Pgubook-readers] memory and addressing
Date: Fri, 14 Jan 2005 00:27:45 +1100

Just to start things off, Great book.Congratulations. I'll show it to my 
teachers at school this year.
But getting on to other things, I wanted to ask a couple of questions, both 
related to the "maximum" program in chapter three.
The first part relates to a question which Tim Hilton asked on this forum 
almost a year ago. After data_items:, Tim uses the label data_end: to mark the 
end of the list. I gather that this is simply calling the assembler to allocate 
the next piece of memory to this label. If this is the case, it would suggest 
that memory is used sequentially ...which has to eventually come to an end, no 
matter how much memory you have. I assume this problem is fixed by re-using 
memory after it has been freed up, but this must mean that it is fragmented and 
so cannot be guaranteed to be sequential in the sense that, if you use one 
chunk of memory, the next piece you ask for may not be the following address. 
So, how can it be assumed that data_end will be anywhere near data_items? Or am 
I completely off the track here?

Here is Tim's program just for reference:

> #PURPOSE:     This program finds the max number
> #
>
> #VARIABLES:   %edi holds index of item being examined
> #             %ebx max data item found
> #             %eax current data item
> #             %ecx holds current data_items pointer
> #             %edx holds ending data_items address
> #
> #             Memory locations:
> #             data_items - contains the data items.
> #             data_end - marks end of data_items addresses
> #             an address is used to terminate the data
> #
>
> .section .data
>
> data_items:           # these are the data items
> .long 3,67,34,222,2,254,54,34,44,1,22,11,8
> data_end:             # marks end address of data_items
> .long 0
>
> .section .text
>
> .globl _start
>
> _start:
> movl $0, %edi                 # move 0 into index register
> movl data_items, %ecx         # initial data_items pointer
> movl data_end, %edx           # end of data_items address
> movl data_items(,%edi,4), %eax        # load the first byte of data
> movl %eax, %ebx                       # %eax is max now
>
> start_loop:                   # start loop
> addl $4, %ecx                 # add 4 to data_items pointer
> cmpl %edx, %ecx                       # see if this is the end address
> je loop_exit                  # if it is exit loop
> incl %edi                     # load next value
> movl data_items(,%edi,4), %eax
> cmpl %ebx, %eax                       # compare values
> jle start_loop                        # jump to the loop beginning
>                               # if %eax is not bigger
> movl %eax, %ebx                       # move %eax as max value
> jmp start_loop                        # jump to loop beginning
>
> loop_exit:
> # %ebx is status code for exit sys call, and contains max number
> movl $1, %eax                 # 1 is exit() sys call
> int $0x80
>
>

Next, my other question relates to how I approached this problem. I guess I'm 
just not sure about what I've done and would like your opinion. I feel that 
it's a bit of a cop-out to tell the program how many data items there are but I 
couldn't see another simple way. It seems to work but I wonder if it's not for 
the wrong reason.
This is my attempt:

 #PURPOSE:      This program finds the highest number from a set of data items.
 #

 #VARIABLES:    The registers have the following uses:
 #
 # %edi -       Holds the infdex of the data item being examined.
 # %ebx -       Largest data item found.
 # %eax -       The current data item.
 #
 #      The following memory locations are used:
 #
 # data_items   Contains the item data. A zero (0) is used to terminate the 
data.
 #

 .section .data

data_items:     #These are the data items.
 .long 3,67,34,223,45,75,54,34,44,33,22,11,66,0

 .section .text

 .globl _start
_start:
 movl $13, %ecx #Places the number 13 (for the 14th digit) into %ecx.
 movl $0, %edi  #Move 0 into the index register.
 movl data_items(,%edi,4), %eax #Load the first byte of data.
 movl %eax, %ebx        #Being the first item, it (%eax) is the biggest.


start_loop:     #Start loop.
 leal data_items(,%ecx,4), %edx #Loads the address of the
                # final data_item into %edx.
 leal data_items(,%edi,4), %esi #Loads the address of the
                # current data_item into %edi.
 cmpl %edx, %esi #Check to see if the two addresses match.
 je loop_exit   #and exit if we have.
 incl %edi      #Increment to the next data_item,
 movl data_items(,%edi,4), %eax #And load it into %eax.
 cmpl %ebx, %eax        #Compare these values.
 jle start_loop #Jump to the loop beginning if the new value
                # isn't bigger.
 movl %eax, %ebx        #Move the value as the largest.
 jmp start_loop #Jump to the loop beginning.


loop_exit:
 # %ebx is the status code for the exit() system call and 
                #it already has the largest value
        movl $1, %eax   #1 is the exit() syscall
        int $0x80

Thanks very much,
Peter.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]