Microprocessors Quiz 3: Return Addresses and Assembly Code, Quizzes of Microprocessors

The solutions to quiz 3 of the ece 332 microprocessors course at boise state university. It explains how to handle return addresses when calling subroutines and provides assembly code examples for performing arithmetic operations and storing results.

Typology: Quizzes

Pre 2010

Uploaded on 09/17/2009

koofers-user-463
koofers-user-463 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1 of 1
Boise State University
Department of Electrical and Computer Engineering
ECE 332 Microprocessors
Quiz 3 โ€“ September 26, 2007
Name: ______________________________
1. You have main, subroutine sub1, and subroutine sub2. main calls subroutine sub1. subroutine sub1 calls
subroutine sub2. What do you need to do with the return addresses in each case so that the program will
complete its execution as designed?
When main calls sub1, the return address (which is pc+4) is automatically saved into ra (or r31) register. In sub1,
before calling sub2, the content of ra register will need to be saved to stack. When sub1 calls sub2, the return
address (from sub2 to sub1) will automatically saved into r31. Our previous save into stack preserved the return
address for sub1 to return to main. After sub2 returns to sub1, we need to restore the return address from stack. This
way, sub1 can return to main correctly.
2. Answer the following questions using this assembly code.
.text
.global _start
_start:
movia r5,VCOUNT
ldw r3,0(r5)
2a. The content of r5 and r7?
movia r7,VALUES address of VCOUNT in r5
address of VALUES in r7
movi r2,0
movi r4,0
loop:
beq r2,r3,loop_end
ldw r6,0(r7)
add r4,r4,r6
addi r7,r7,4 2b. Provide the mathematical expression for this
addi r2,r2,1 segment of code.
br loop
if you have r4 = 1+2+3+4 you are okay or
loop_end: r4 =
r4=mem(i)
i=0
3
๎˜
movia r7,SUM
stw r4,0(r7)
.data
VCOUNT: .word 4
VALUES: .word 1,2,3,4,5
SUM: .word 0 2c. The value of SUM after execution of โ€œstw r4,0(r7)โ€?
.end 10
2d. Why we add โ€œ4โ€ in โ€œadd r7,r7,4โ€?
Because VALUES is declared as .word, in this architecture, it is 32 bits or 4 bytes. The 4 corresponds to 4 bytes for
go to the next word. To go to the next memory location to access the new value.

Partial preview of the text

Download Microprocessors Quiz 3: Return Addresses and Assembly Code and more Quizzes Microprocessors in PDF only on Docsity!

1 of 1

Boise State University

Department of Electrical and Computer Engineering

ECE 332 Microprocessors

Quiz 3 โ€“ September 26, 2007

Name: ______________________________

1. You have main, subroutine sub1, and subroutine sub2. main calls subroutine sub1. subroutine sub1 calls

subroutine sub2. What do you need to do with the return addresses in each case so that the program will

complete its execution as designed?

When main calls sub1, the return address (which is pc+4) is automatically saved into ra (or r31) register. In sub1,

before calling sub2, the content of ra register will need to be saved to stack. When sub1 calls sub2, the return

address (from sub2 to sub1) will automatically saved into r31. Our previous save into stack preserved the return

address for sub1 to return to main. After sub2 returns to sub1, we need to restore the return address from stack. This

way, sub1 can return to main correctly.

2. Answer the following questions using this assembly code.

.text

.global _start _start:

movia r5,VCOUNT ldw r3,0(r5)

2a. The content of r5 and r7?

movia r7,VALUES address of VCOUNT in r address of VALUES in r movi r2, movi r4,

loop: beq r2,r3,loop_end ldw r6,0(r7) add r4,r4,r

addi r7,r7,4 2b. Provide the mathematical expression for this

addi r2,r2,1 segment of code.

br loop if you have r4 = 1+2+3+4 you are okay or

loop_end: r4 = r 4 = mem ( i ) i = 0

3

movia r7,SUM stw r4,0(r7) .data

VCOUNT: .word 4 VALUES: .word 1,2,3,4,

SUM: .word 0 2c. The value of SUM after execution of โ€œstw r4,0(r7)โ€?

.end 10

2d. Why we add โ€œ 4 โ€ in โ€œadd r7,r7,4โ€?

Because VALUES is declared as .word, in this architecture, it is 32 bits or 4 bytes. The 4 corresponds to 4 bytes for

go to the next word. To go to the next memory location to access the new value.