Sie sind auf Seite 1von 2

Transfiguration tips and tricks

November 22, 2015


Transfiguration is some of the most complex and dangerous magic you will learn at Hogwarts.
Anyone messing around in my class will leave and not come back. You have been warned."
Minerva McGonagal

Transfiguration of generic assembly code to Nios II

Appendix B of the course textbook gives an overview of Nios II architecture and its instruction set. The
problems in the end of the chapter are mostly write Nios II assembly code for problem so-and-so in Chapter
2 or Chapter 3. However, chapters 2 and 3 are presented using generic assembly or a pseudoassembly. This
can lead to confusion when attempting to translate one to the other, especially since you are supposed to use
Nios II assembly in the exam.
Luckily, the generic assembly used in the aforementioned chapters is quite similar to the one in Nios II,
not just because it is a 32-bit word instruction set, but also because it shares the same philosophy.
Let us go through a code in Chapter 2, for instance the one in Figure 2.8. In Nios II its equivalent is in
listing given in Algorithm 1. Every instruction can be replaced with a suitable instruction from Nios II, and
it is usually straightforward.1
Another example is Algorithm 2, which gives a listing of Nios II code for Figure 2.24 from the book.
You can practice this by converting the code from section 2.15 (Solved Problems) to Nios II code.

Transfiguration of Nios II (or generic assembly code) to C

C is the industry standard for embedded programming, and youll see it compile on very different architectures, from microcontrollers to supercomputers. This is why you need to get used to it as soon as possible.
Chapter 4 of the book is sort of a baptism by fire in this sense, as you have to manage interrupts from C. It
is actually simplifying your coding process, but at this time it may look terrifying.
1 Caveat: instructions like addi and add from Nios II are denoted by single instruction ADD in generic assembly. You need to
deduce which one to use by observing the argument they take. If it is #constant_name or a number, then you have to use addi.

Algorithm 1 Nios II version of code in Fig. 2.8 in the book


ldw r2 , N( r 0 )

#or , i f needed movia r5 , N ldw r2 , ( r 5 )


#s o l o a d i s j u s t ldw
mov r3 , r 0
#s o c l e a r i s j u s t mov r 0 t o a r e g i s t e r
movi r4 , NUM1
#move a #v a l u e i s j u s t movi
LOOP:
#l a b e l s a r e t h e same
ldw r5 , ( r 4 )
#again , l o a d i s ldw
add r3 , r3 , r 5
#add i s add
a d d i r4 , r4 , 4
#adding a c o n s t a n t i s a d d i
s u b i r2 , r2 , 1
#s u b s t r a c t i n g a c o n s t a n t i s s u b i
bgt r2 , r0 ,LOOP #b r a n c h i n g i s bgt , b l t , beq , u s u a l l y with r 0
stw r3 , SUM( r 0 ) #o r with movia and stw , i f needed

Algorithm 2 Nios II version of code in Fig. 2.24 in the book


movia r2 , LOC
#moving a 32 b i t c o n s t a n t , hence movia
l d b r3 , ( r 2 )
#LoadByte i s l d b
s l l i r3 , r3 , 4 #L S h i f t L i s s l l i b e c a u s e o f c o n s t a n t 4
a d d i r2 , r2 , 1 #add i s a d d i
l d b r4 , ( r 2 )
#again , l d b
a n d i r4 , r4 , 0 xF #and i s a n d i b e c a u s e o f c o n s t a n t 0xF
o r r3 , r3 , r 4
#o r i s o r b e c a u s e i t s done on r e g i s t e r s
s t b r3 , PACKED( r 0 )
Algorithm 3 C version of code in Fig. 3.18 in the book
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

/ D e f i n e r e g i s t e r a d d r e s s e s /
#define DIGIT ( v o l a t i l e char ) 0 x800
/ so , a l l EQU a r e #d e f i n e /
#define SEVEN ( v o l a t i l e char ) 0 x4030 / u s e v o l a t i l e t o k e e p t h e p o i n t e r s /
void main ( )
{
char ch ;
/ h e r e we l l l o a d t h e d i g i t , R2 i n a s s e m b l y /
char d i s p ;
/ h e r e we l l p u t t h e p a t t e r n t o be d i s p l a y e d , R5 i n a s s e m b l y /
const char t a b l e [ 1 6 ] = { 0 x7e , 0 x30 , 0 x6d , 0 x79 , 0 x33 , 0x5b , 0 x5f , 0 x70 , 0 x7f , 0x7b ,
0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 , 0 x00 } ;
/ u s i n g an a r r a y as a l o o k u p t a b l e g i v e n by TABLE i n a s s e m b l y /
ch = DIGIT ;
/ a s s i g n m e n t i s l o a d i n g from memory , i . e . l d b /
i f ( ( ch & 0 x f 0 )!=0 x30 ) ch=0x 0 f ; / i f t h e f i r s t n i b b l e o f ch i s n o t 0011 , ch i s n o t a d i g i t /
e l s e ch=ch & 0 x 0 f ;
/ e l s e , e x t r a c t t h e d i g i t from i t /
d i s p = t a b l e [ ch ] ;
SEVEN = d i s p ;
}

Let us take a look at the problems section in Chapter 4. First few problems ask for C-code for problems
3.2, 3.9, 3.13, 3.15, 3.17. All those Chapter 3 problems are concerned with display of some numbers on
different types of displays. Solved problems in Chapter 3 can help in understanding how is this done in
assembly (also use your laboratory materials, as youve used 7-segment displays to display BCD digits!)
However, the challenge is to convert it to C. Lets take the code given in Figure 3.18 (Example 3.6) and
write C code for it (Algorithm 3). We wont invent anything, just look at code in Figure 4.3 and structure
our own code in the same fashion.
Lets now see what we did in that code, line by line with Figure 3.18.
1. First two lines are substituted with #define statements in our code (lines 2 and 3).
2. Third line is substituted with assignment in line 10.
3. Lines 4, 6, 7 and 8 are substituted with conditional statement in line 11. Logic in assembly code was
compare upper nibble with 3. If it is equal, go to display procedure. If it is not, first say that the
caracter is not a number so the display can display a blank. Here we did compare upper nibble with
3. If it is not equal, say it is not a number so the display can display a blank.
4. Line 5 is substituted with line 13. It is performed only if the character is a digit, i.e. if the upper nibble
is 3.
5. Lines 9 and 10 are lines 14 and 15, respectively.
6. The TABLE from memory is placed in an array with line 8. It could also be placed in memory using
#define, but const char will do the trick as well.
I have been using char as the variable type assuming it is a one-byte type. Dont forget that char is essentially
an int.

Das könnte Ihnen auch gefallen