ECE 330 Homework 6 Fall 2010 Due: 10/04/2010

advertisement
ECE 330
Due: 10/04/2010
Homework 6
Fall 2010
1. Observe the output for the code in Code 1 below and answer the following questions.
// file: hw6.c
#include <stdio.h>
int main()
{
unsigned
int
unsigned
unsigned
unsigned
int
wa[] = { 0x41424344, 0x51525354, 0x61626364, 0x0 };
i;
int
*pwa;
short *psa;
char *pba;
puts("ECE 330 Homework 6");
pwa = (unsigned int *)
&wa;
psa = (unsigned short *) &wa;
pba = (unsigned char *) &wa;
// Loop 1
for ( i=0; i<sizeof(wa)/sizeof(wa[0]); i++ )
printf ( "wa[%d]=0x%08x\n", i, wa[i] );
// Loop 2
while ( *pwa != 0 )
printf ( "%x\n", *pwa++ );
// Loop 3
while ( *psa != 0 )
printf ( "%x\n", *psa++ );
// Loop 4
while ( *pba != 0 ) {
printf ( "%c[%x] ", *pba, *pba );
pba++;
}
putchar('\n');
printf("\nall done\n");
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 3
ECE 330
Due: 10/04/2010
Homework 6
Fall 2010
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)
# store SUM
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
# 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 3
ECE 330
Due: 10/04/2010
3.
Homework 6
Fall 2010
Write assembly language functions getbits and putbits that conform to the following
API definitions (prototypes).
typedef unsigned int ui32;
ui32 getbits (ui32 word, ui32 offset, ui32 nbits);
Returns requested bits right justified.
ui32 putbits (ui32 word, ui32 offset, ui32 nbits, ui32 newbits);
Returns input word (arg1) with newbits (arg4) replacing bits in specified location.
Provide C language test code to demonstrate usage of the two functions.
Page 3 of 3
Download