Yet More Programming

15 September

Correction

#char S1[] = "This is a string."
#int Foo[16];
#char S2[100];

     .data
S1:  .asciiz "This is a string."
     .align 2    # get to a 4 byte boundary     
Foo: .space 64   # room for 16 ints
S2:  .space 100  # destination string

SDRAM

clear1

void clear1(int array[], int size) {
  for(int i=0; i<size; i++)
    array[i] = 0;
}
   move $t0,$zero # i = 0
for1:
   slt  $t1, $t0, $a1
   beq  $t1, $zero, for2 # break if i >= size
   muli $t1, $t0, 4      # t1 = i*4 (pseudo)
   add  $t1, $a0, $t1    # t1 = &array[i]
   sw   $zero, 0($t1)    # *t1 = 0
   addi $t0, $t0, 1      # i++
   j    for1
for2:
   jr   $ra

clear2

void clear2(int *array, int size) {
  for(int *p = &array[0]; p < &array[size]; p++)
     *p = 0;
}
   move $t0, $a0 # p = array
for1:
   muli $t1, $a1, 4      # t1 = 4*size
   add  $t1, $a0, $t1    # t1 = &array[size]
   slt  $t2, $t0, $t1
   beq  $t2, $zero, for2 # break if p >= t1
   sw   $zero, 0($t0)    # *p = 0;
   addi $t0, $t0, 4      # p++
   j    for1
for2:
   jr   $ra

clear2 (slightly smarter)

void clear2(int *array, int size) {
  for(int *p = &array[0]; p < &array[size]; p++)
     *p = 0;
}
   move $t0, $a0 # p = array
   muli $t1, $a1, 4      # t1 = 4*size
   add  $t1, $a0, $t1    # t1 = &array[size]
for1:
   slt  $t2, $t0, $t1
   beq  $t2, $zero, for2 # break if p >= t1
   sw   $zero, 0($t0)    # *p = 0;
   addi $t0, $t0, 4      # p++
   j    for1
for2:
   jr   $ra

clear3

void clear3(int *array, int size) {
   int *arrayend = array + size;
   while(array < arrayend) *array++ = 0;
}

   muli $t1, $a1, 4   # t1 = 4*size
   add  $t1, $a0, $t1 # t1 = &array[size]
for1:
   slt  $t2, $a0, $t1
   beq  $t2, $zero, for2 # break if array >= t1
   sw   $zero, 0($a0)    # *array = 0;
   addi $a0, $a0, 4      # array++
   j    for1
for2:
   jr   $ra

Pointer Summary

Big Constants

The MIPS architecture only allows immediate constants to be 16 bits. So how do we get bigger constants?

How to break your BIG number into the required two 16 bit chunks?

hi = BIG / 64k (e.g. 4,000,000 / 64k == 61)
lo = BIG % 64k (e.g. 4,000,000 % 64k == 2304)

lui $t0, hi
ori $t0, $t0, lo

Pointer Size versus Addressable Space

Endian?

Consider the following code:

foo: .word 0      # foo is a 32 bit int
   li $t0, 1      # t0 = 1
   la $t1, foo    # t1 = &foo
   sb $t0, 0($t1) # stores a byte at foo

What is the value of the WORD at foo?

Endian?

Consider the following code:

foo: .word 0      # foo is a 32 bit int
   li $t0, 1      # t0 = 1
   la $t1, foo    # t1 = &foo
   sb $t0, 1($t1) # stores a byte at foo+1

What is the value of the WORD at foo?

Endian?

Consider the following code:

foo: .word 0      # foo is a 32 bit int
   li $t0, 1      # t0 = 1
   la $t1, foo    # t1 = &foo
   sb $t0, 2($t1) # stores a byte at foo+2

What is the value of the WORD at foo?

Endian?

Consider the following code:

foo: .word 0      # foo is a 32 bit int
   li $t0, 1      # t0 = 1
   la $t1, foo    # t1 = &foo
   sb $t0, 3($t1) # stores a byte at foo+3

What is the value of the WORD at foo?

Endian?

Consider the following code

foo: .ascii “Gary” # foo takes 4 bytes, 32 bits
   la $t1, foo     # t1 = &foo
   lw $t2, 0($t1)  # what is in t2?

Little Endian: t2 == 0x79726147
Big Endian: t2 == 0x47617279

On BOTH machines:

   lb  $t3, 0($t1) # t3 == ‘G’

ASCII Chart

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

0

NUL

SOH

STX

ETX

EOT

ENQ

ACK

BEL

BS

HT

LF

VT

FF

CR

SO

SI

1

DLE

DC1

DC2

DC3

DC4

NAK

SYN

ETB

CAN

EM

SUB

ESC

FS

GS

RS

US

2

SP

!

"

#

$

%

&

'

(

)

*

+

,

-

.

/

3

0

1

2

3

4

5

6

7

8

9

:

;

<

=

>

?

4

@

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

5

P

Q

R

S

T

U

V

W

X

Y

Z

[

\

]

^

_

6

`

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

7

p

q

r

s

t

u

v

w

x

y

z

{

|

}

~

DEL

Hex

Hex

?

What is difference between: "DEADBEEF" and 0xDEADBEEF?

Choosing Registers

Choosing Registers

pseudo instructions

pseudo instructions

move $t1,$t0

add $t1, $t0, $zero

la $t0,foo

lui $t0, UPPER16(foo)

ori $t0, LOWER16(foo)

li $t0, 23

addi $t0, $zero, 23

li $t0, 0x2300AB

lui $t0, 0x23

ori $t0, 0x00AB

Study Abroad Fair

StudyAbroad.png

Geek Highlight

Check out /[WWW] http://www.tiddlywiki.com

last edited 2005-09-15 17:17:03 by GaryBishop