jack bloom
jack bloom

Reputation: 45

C to NIOS II program

I need to write the following C code in NIOS II assembly code. and know the stack state from the L1 label.

struct lelt 
{
   int value; 
   struct lelt* next;
}

struct lelt x = {3,NULL};

lelt* get_tail(lelt *ptr)
{
   lelt* last; 
   L1: last = NULL; 
   while(ptr != NULL)
   {
      last = ptr; 
      ptr = ptr -> next; 
   }
   return last; 
}

Here's what I've wrote till now but I don't know how to manage the write the rest of the code because I'm not very familiar with the structs in assembly code. So if someone can help me out and explain a little bit I'd be grateful.

x: 
 value .word 3
 next .word 0
get_tail: 
 subi sp, sp,12
 stw ra, 0(sp) 
 stw r16, 4(sp) // ptr
 stw r17, 8(sp) // last
 movia r16,zero,r4
 bne r4,zero,endwhile
 add r17,zero,r16
 ... // i don't know how to write ptr=ptr->next 
endwhile: 
 add r2,r17,zero
 br end
end: 
 ldw ra,0(sp)
 ldw r16,4(sp)
 ldw r17,8(sp) 
 addi sp,sp,12
 ret 

Upvotes: 2

Views: 486

Answers (1)

Jester
Jester

Reputation: 58762

So I guess r16 is your ptr. The next member in the struct is at offset 4. To load that into ptr, just do ldw r16, 4(r16).

Upvotes: 1

Related Questions