Procedures; Number Representation

Tom Kelliher, CS 220

Sept. 24, 1997

Announcements

How to use script.

Homework due Friday.

Assignment

Finish reading Chapter 3.

Procedures

What is the nature of a procedure?

Steps in executing a procedure:

  1. Deposit parameters.

  2. Save return address.

  3. Call procedure.

  4. Execute procedure.

  5. Deposit return value.

  6. Return.

How do we implement this within our memory model?

Example

f1, a function:

            .data
f1p1:       .word    # parameter 1 of function 1
f1p2:       .word    # parameter 2 of function 1
...                  # other parameters and locals
f1rv:       .word    # return value of function 1
f1ra:       .word    # return address of function 1

            .text
f1:         ...               # first instruction of f1
            move f1rv, ...    # store return value
            b (f1ra)          # return

To use f1:

            .text
            ...
            move f1p1, ...    # load parameters
            ...
            la f1ra, ret1     # save return address
            b f1              # call f1
ret1:       move ..., f1rv    # instruction to which control returns;
                              # extract return value

  1. Can we implement recursion?

  2. Indirect addressing and the b instruction.

  3. la instruction.

  4. Ok to have multiple .data, .text segments.

    Can two procedures use the same name for a local variable?

Another Example

Write a procedure to swap two .words. Write a code fragment to call the procedure.

Number Representation

Properties of conventional number systems:

  1. Positional.

  2. Weighted.

  3. Radix/Base.

  4. Most significant, least significant.

Unsigned value of a binary integer:

  1. n bit number.

  2. Bit numbering.

Base Conversions

Common bases: decimal, binary, octal, hexadecimal.

Binary to Decimal

Use the previous summation and do all math in decimal.

Example: convert binary 110101 to decimal.

(How well do you know your powers of 2?)

Decimal to Binary

Algorithm:

let dnum be the decimal number to convert;
index = 0;
bnum = 0;

while (dnum != 0)
{
   bnum[index] = dnum % 2;
   dnum = dnum / 2;
   index = index + 1;
}

Example: convert decimal 133 to binary.

Binary to Hex/Octal and Vice Versa

Form for octal & hexadecimal constants in C?

Conversion examples?



Thomas P. Kelliher
Tue Sep 23 19:39:33 EDT 1997
Tom Kelliher