ECE 330 Homework 7 Spring 2011 Due: 3/11/2011

advertisement
ECE 330
Due: 3/11/2011
Homework 7
Spring 2011
1. Observe the output for the code in Code 1 below and answer the following questions.
// file: hw7.c
#include <stdio.h>
int main()
{
int
unsigned int
unsigned int
unsigned short
unsigned char
i;
wa[] = { 0x41424344, 0x51525354, 0x61626364, 0x0 };
w;
s;
b;
unsigned int
*pwa;
unsigned short *psa;
unsigned char *pba;
pwa = (unsigned int *)
&wa;
psa = (unsigned short *) &wa;
pba = (unsigned char *) &wa;
// Loop 1
for ( i=0; i<sizeof(wa)/sizeof(wa[0]); i++ )
w = wa[i];
// Loop 2
while ( *pwa != 0 )
w = *pwa++;
// Loop 3
while ( *psa != 0 )
s = *psa++;
// Loop 4
while ( *pba != 0 )
b = *pba++;
return 0;
}
Code 1.
For each of the loops (4) in Code 1. capture the assembly code produced. Note how the
pointers (pwa, psa, pba) are initialized and incremented for each.
Questions.
a. In loops 2-4, do the pointer increments work the same? If not, explain and show
assembly instructions to justify.
b. In loops 2 and 3, what is being incremented? The value being pointed at or the
pointer?
c. When the program has completed, what are the pointers pointing at? If the
pointers are to be used again will they need to be re-initialized?
d. Explain the usage of the sizeof keywords in the for statement of Loop 1.
e. How does the (*pw? != 0) control the looping in loops 2-4?
f. Explain the output from loop 4.
Page 1 of 2
ECE 330
Due: 3/11/2011
Homework 7
Spring 2011
2. In homework 4 you were asked to write the function sum that could be called by the
following assembly code. For this problem, re-write the following code and the sum
code you produced in homework 4 using the C programming language. The new top
level (main) code should call both the assembly and C language versions and display
results in order to compare output.
/*
* File:
* Author:
* Date:
* Processor:
* Environment:
*/
test_sum.s
BSU ECE330 Examples
September 2007
Nios2_r1x.ptf
Altera Debug Client
.include "system.inc"
.text
.global _start
_start:
movia
movia
sp, STACK
r23,DSECT
addi
ldw
call
stw
r4,r23,VALUES-DSECT
r5,COUNT-DSECT(r23)
sum
r2,SUM-DSECT(r23)
mov
addi
r4,r2
r5,r23,PBUFF-DSECT
# address of PBUFF
call
itoh
# convert binary to printable
addi
r4,r23,PBUFF-DSECT
# address of PBUFF
call
bsu_prints
br
.
# address of values array
# get count
# store SUM
# Halt execution
.data
DSECT:
COUNT:
SUM:
VALUES:
VALUES_END:
PBUFF:
.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
Notes:
• Use a header file containing function prototypes so that the compiler can verify
definition of functions and usage.
•
Supply all source code and a display of results. (code must be documented)
•
Supply a display of results that clearly shows results from both versions of sum
routines. Parameters passed and results returned for both versions should be the
same.
Page 2 of 2
Download