ECE 330 Homework 4 Spring 2011 Due: 2/16/2011

advertisement
ECE 330
Due: 2/16/2011
Homework 4
Solution
Spring 2011
1. Which routines require usage of the stack?
# a.s
.text
.global _start
_start:
...
call b
.text
.global b
b:
...
call c
...
br
# b.s
...
.
ret
.end
# c.s
.text
.global c
c:
...
# d.s
.text
.global d
d:
...
call d
call e
...
...
ret
ret
.end
.end
# e.s
.text
.global e
e:
...
ret
.end
.end
Routines b, c and d are all required to save at least the return address register (ra) on
the stack. Routines _start, b, c and d should use registers r16-r23 and routine e should
use registers r8-r15.
2. Use the supplied assembly include file to answer the following questions.
# system.inc
.equ MEMORY_START,0x20000
.equ MEMORY_SIZE,131072
.equ STACK,MEMORY_START+MEMORY_SIZE
...
a. Provide code to initialize the stack.
movia sp,STACK
b. Provide code to push registers r17,r19, r21 and ra onto the stack.
subi
stw
stw
stw
stw
sp,sp,16
ra,0(sp)
r17,4(sp)
r19,8(sp)
r21,12(sp)
c. Provide code to pop the registers pushed in part b.
ldw
ldw
ldw
ldw
addi
ra,0(sp)
r17,4(sp)
r19,8(sp)
r21,12(sp)
sp,sp,16
Page 1 of 4
ECE 330
Due: 2/16/2011
Homework 4
Solution
Spring 2011
3. Use the following code snippets for the following questions.
# system.inc
.equ MEMORY_START,0x40000
.equ MEMORY_SIZE,131072
.equ STACK,MEMORY_START+MEMORY_SIZE
...
# a.s
# b.s
# c.s
# d.s
# e.s
.include “system.inc”
.text
.text
.text
.text
.text
b: .global b
c: .global c
d: .global d
e: .global e
_start: .globl _start
subi sp,sp,20
subi sp,sp,12
subi sp,sp,16
movia sp,STACK
...
...
...
...
call c
call d
call e
call b
...
...
...
addi sp,sp,20
addi sp,sp,12
addi sp,sp,16
ret
ret
ret
...
br
.
.end
.end
.end
.end
...
ret
.end
In the example above, assume that the current program counter is pointing to code in function e.
routine
_start
b
c
d
e
sp
0x60000
0x5FFEC
0x5FFE0
0x5FFD0
0x5FFD0
owns
none
0x5FFEC - 0x5FFFF
0x5FFE0 - 0x5FFEB
0x5FFD0 – 0x5FFDF
none
a. What are the byte addresses of stack space owned by function b?
0x5FFEC - 0x5FFFF
b. What are the byte addresses of stack space owned by function c?
0x5FFE0 - 0x5FFEB
c. What are the byte addresses of stack space owned by function d?
0x5FFD0 – 0x5FFDF
d. What is the current value of the stack pointer (sp)?
0x5FFD0
Page 2 of 4
ECE 330
Due: 2/16/2011
Homework 4
Solution
Spring 2011
4. Create a function called strcpy with the following instructions.
Memory tab of Altera Debug Client
# file: test_strcpy.s
.text
_start:
.global _start
movia
sp, STACK
movia
movia
call
r5,NAME
r4,TO
strcpy
PROGRAM_END:
break
br
PROGRAM_END
.data
NAME:
TO:
.end
.word 0xbbbbbbbb
.string "your name here"
.fill 0x4,1,0xee
.word 0xaaaaaaaa
.fill 40,1,0xa5
.word 0xffffffff
Figure 2.
Following is a snapshot of memory before executing strcpy.
Supply a similar snapshot after executing strcpy that shows your name included in memory.
Provide code with homework.
.text
# function: strcpy
#
c syntax: void strcpy (char *, char *);
#
register usage:
#
r4: pointer to target string
#
r5: pointer to source string
.global strcpy
strcpy:
mov
mov
r8,r4
r9,r5
# copy target pointer
# copy source pointer
loop:
ldb
stb
beq
addi
addi
br
r7,0(r9)
r7,0(r8)
r7,r0,loop_end
r8,r8,1
r9,r9,1
loop
#
#
#
#
#
#
load character from source
store character to target
is this terminating char?
point to next source char
point to next target location
return for next character
loop_end:
# copy completed
ret
# return to caller
.end
Page 3 of 4
ECE 330
Due: 2/16/2011
Homework 4
Solution
Spring 2011
5. Using the following top level code, create a module sum.s that will compute the sum of
values passed. Note the setting up of registers before calling the function.
# test_sum.s
.include "system.inc"
.text
.global _start
_start:
movia
sp, STACK
movia
movia
ldw
call
r4,VALUES
r5,COUNT
r5,0(r5)
sum
movia
stw
r8,SUM
r2,0(r8)
# store SUM
mov
movia
call
r4,r2
r5,BUFF
itoh
# convert binary to printable
mov
r4,r5
# address of PBUFF
call
bsu_prints
br
.
# address of values array
# get count
# Halt execution
.data
COUNT:
SUM:
VALUES:
VALUES_END:
BUFF:
.word (VALUES_END-VALUES)/4
.word ~0
.word 3,2,7,9,4,11
.word 0,0,0,0,0
.fill 20,1,0xff
.end
# file: sum.s
.text
# -----------------------------------------# Sum array of integers
# C syntax: int sum ( int a[], int count );
# Register usage:
#
r2: sum (return value)
#
r4: passed pointer to values
#
r5: passed count
#
# -----------------------------------------sum: .global sum
mov
r2,r0
# initialize sum
loop:
beq
r5,r0,loop_end
# end of array?
ldw
r8,0(r4)
# get next value
add
r2,r2,r8
# add to sum
addi
r4,r4,4
# point to next value
subi
r5,r5,1
# decrement count
br
loop
# go get next element
loop_end:
ret
# return with sum in r2
.end
Page 4 of 4
Download