Lecture 9: Assembly Language

1 / 16

# Lecture 9: Assembly Language - PowerPoint PPT Presentation

Lecture 9: Assembly Language. Computer Engineering 211 Spring 2002. 0x7fff0000. E(LSB). 0x7ffefffc. E(MSB). 0x7ffefff8. Parameter vector. Passing parameters on stack. int A, B, C, D, E; char F; foo(A, B, C, D, E, F);. A (\$s0), B (\$s1), C (\$s2), D (\$s3) E (\$s4), F (\$s5).

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Lecture 9: Assembly Language' - norah

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Lecture 9: Assembly Language

Computer Engineering 211

Spring 2002

0x7fff0000

E(LSB)

0x7ffefffc

E(MSB)

0x7ffefff8

Parameter vector

Passing parameters on stack

int A, B, C, D, E;

char F;

foo(A, B, C, D, E, F);

A (\$s0), B (\$s1), C (\$s2), D (\$s3)

E (\$s4), F (\$s5)

Calling context:

----------

move \$a0, \$s0

move \$a1, \$s1

move \$a2, \$s2

move \$a3, \$s3

sw \$s5, -4(\$sp)

sw \$s4, -4(\$sp)

jal foo

Stack

before foo call

\$sp

F

Stack

before foo call

0x7ffefff8

\$sp

F

E

\$ra

0x7ffefff4

Passing parameters on stack, Contd.

Character ‘0’, ASCII code: 0x30

foo (AA, BB, CC, DD, EE, FF)

int AA, BB, CC, DD, EE;

char FF;

{

if (FF != ‘0’)

AA = AA + BB + CC + DD;

else

AA = EE;

}

foo: sw \$ra, -4(\$sp)

lw \$t0, 8(\$sp)

#\$t0 contains FF

li \$t1, 0x30

beq \$t0, \$t1, elsepart

j join

elsepart: lw \$a0, 4(\$sp)

join: lw \$ra, 0(\$sp)

jr \$ra

Stack

before foo call

\$sp

F

E

0x7ffefff8

Passing parameters on stack, Contd.

Calling context:

----------

move \$a0, \$s0

move \$a1, \$s1

move \$a2, \$s2

move \$a3, \$s3

sw \$s5, -4(\$sp)

sw \$s4, -4(\$sp)

jal foo

move \$s0, \$a0

A by Value

Call by Value/reference for Register parameters

Interpret at the calling context end!

A by reference:

-----

-----

jal foo

move \$s0, \$a0

int A, B, C, D, E;

char F;

foo(A, B, C, D, E, F);

foo (AA, BB, CC, DD, EE, FF)

int AA, BB, CC, DD, EE;

char FF;

{

if (FF != ‘0’)

AA = AA + BB + CC + DD;

else

AA = EE;

}

Stack

before foo call

\$sp

F

E

\$ra

0x7ffefff4

E

30

F

10

\$gp

Global Data

Call by Value/reference for Stack parameters

# EE & FF by value

foo: sw \$ra, -4(\$sp)

lw \$t0, 8(\$sp)

#\$t0 contains FF

li \$t1, 0x30

beq \$t0, \$t1, elsepart

j join

elsepart: lw \$a0, 4(\$sp)

join: lw \$ra, 0(\$sp)

jr \$ra

Interpret at the called procedure end!

lw \$s4, 30(\$gp); lw \$s5, 10(\$gp)

Stack

before foo call

\$sp

E

\$ra

0x7ffefff4

E

30

F

10

\$gp

Call by reference for Stack parameters

# FF by reference

foo: sw \$ra, -4(\$sp)

lw \$t0, 8(\$sp)

lw \$t0, 0(\$t0)

#\$t0 contains FF

li \$t1, 0x30

beq \$t0, \$t1, elsepart

j join

elsepart: lw \$a0, 4(\$sp)

join: lw \$ra, 0(\$sp)

jr \$ra

Stack

before foo call

\$sp

E

\$ra

0x7ffefff4

E

30

F

10

\$gp

Call by reference for Stack parameters

Calling context:

----------

move \$a0, \$s0

move \$a1, \$s1

move \$a2, \$s2

move \$a3, \$s3

la \$t0, 10(\$gp)

sw \$t0, -4(\$sp)

sw \$s4, -4(\$sp)

jal foo

move \$s0, \$a0

Activation Record/Stack Frame
• Stack frame/activation record (AR) created for each procedure call/activation.
• What goes into it?
• Caller: parameter vector
• Callee: saved registers (spilled registers).
• Callee: local variables
Register Saving

\$t0-\$t9:Temporaries – caller has no guarantee

that the called procedure will not modify them.

\$s0-\$s7:Saved Temporaries – caller is guaranteed

that the called procedure will retain their value on

return.

• Caller saves the \$t0-\$t9 needed after a procedure call

on the stack before initiating the call.

• Callee saves the \$s0-\$s7 it wants to use, and restore

them before returning from the call.

Register saving

A: \$t0, B: \$t1, C:\$s0,

D:\$s1, E: \$s2, F:\$s3,

Z: \$s4

int A, B, C, D, E, F, Z;

---

Z=SUM(A, B, C, D, E, F)

---

#save any temp registers

sw \$t0, -4(\$sp) move \$a2, \$s0

sw \$t1, -8(\$sp) move \$a3, \$s1

#stack parameters jal SUM

sw \$s3, -12(\$sp) move \$s4, \$v0

sw \$s4, -16(\$sp)

#reg parameters

move \$a0, \$t0

move \$a1, \$t1

Caller saved

Register saving

SUM: sw \$ra, -4(\$sp)

# read E & F into \$s0 & \$s1 add \$v0, \$v0, \$t0

sw \$s0, -8(\$sp) #restore \$s0 & \$s1

sw \$s1, -12(\$sp)lw \$s0, 4(\$sp)

addi \$sp, \$sp, -12 lw \$s1, 0(\$sp)

lw \$s0, 12(\$sp) # load E # restore \$ra

lw \$s1, 16(\$sp) # load F lw \$ra, 8(\$sp)

add \$t0, \$a2, \$a3 jr \$ra

Callee saved

\$t0

\$t1

F

10000

E

\$sp

\$ra

9996

\$s0

9988

\$s1

Stack Growth in SUM

SUM: sw \$ra, -4(\$sp)

sw \$s0, -8(\$sp)

sw \$s1, -12(\$sp)

lw \$s0, 12(\$sp)

lw \$s1, 16(\$sp) # load F

lw \$s0, 4(\$sp)

lw \$s1, 0(\$sp)

lw \$ra, 8(\$sp)

jr \$ra

Result on the Stack

int A, B, C, D, E, F, Z;

---

X,Y,Z=MAX3(A, B, C, D, E, F)

A: \$t0, B: \$t1, C:\$s0,

D:\$s1, E: \$s2, F:\$s3,

X:\$s4, Y:\$s5, Z: \$s6

---

#save any temp registers

sw \$t0, -4(\$sp) move \$a2, \$s0

sw \$t1, -8(\$sp) move \$a3, \$s1

#stack parameters jal MAX3

sw \$s3, -12(\$sp)

sw \$s4, -16(\$sp)

#reg parameters

move \$a0, \$t0

move \$a1, \$t1

Result on the Stack, Contd.

MAX3: sw \$ra, -4(\$sp)

addi \$sp, \$sp, -4 lw \$ra, 0(\$sp)

lw \$s1, 8(\$sp) # load F jr \$ra

# a bunch of if-then-else

# place MAX1: \$t0, MAX2: \$t1, MAX3: \$t3

# return the first two words in \$v0 & \$v1

move \$v0, \$t0

move \$v1, \$t1

# the extra word returned on stack

# overwrites the parameter vector

sw \$t3, 4(\$sp)

Result on the Stack, Contd.

A: \$t0, B: \$t1, C:\$s0,

D:\$s1, E: \$s2, F:\$s3,

X:\$s4, Y:\$s5, Z: \$s6

---

#save any temp registers

sw \$t0, -4(\$sp) jal SUM

sw \$t1, -8(\$sp) #retrieve result

#stack parameters move \$s4, \$v0

sw \$s3, -12(\$sp) move \$s5, \$v1

sw \$s4, -16(\$sp) lw \$s6, 0(\$sp)