Hi I was wondering if anyone could help me with My university assignment. In this part we are required to write a program to calculate the tower of Hanoi number using a for-loop within a subroutine. The C-code is provided.

typedef unsigned char byte; // define 8-bit variable type

int main(void) {

byte result, num = 6;

for( ;; ) {

// Calculate the result of the Tower of Hanoi

result = hanoi(num);

}

}

byte hanoi(byte n){

// calculate 2^n-1 without the pow() function

byte ret = 1;

for(byte i = 1; i <= n; i++) {

ret *= 2;

}

ret--;

return ret;

}

I need to implement this code using assembler language, and using the stack.

I started of with the basic

.include "m8515def.inc" ; include the ATMEGA8515(L) definitions file

.def NUM = r16 ; use this register to store "˜num'

.def RESULT = r17 ; use this register to store "˜result'

.def temp = r18

RESET:

LDI temp, high (RAMEND)

OUT SPH, temp

LDI temp, low (RAMEND)

OUT SPL, temp

This is where im now stuck, as after numersous hours of research, I am still not sure exactly how to implement the task at hand.

The specifications are that

LOOP:

ldi num, 0x06 ; this is our start number

*** setup passing parameters on the stack ***

rcall HANOI

*** capture the return value from the stack ***

rjmp LOOP ; Jump back to start

HANOI:

*** Add hanoi calculation ***

ret

.

I have tried multiple things such as PUSH NUM, PUSH RESULT, but my lecture notes, as well as other material confuses me, such as why is push used again in the function, and why are there different variables sometimes.

I also know i have to use LDD and also LSL to multiply by 2, but i just dont get at all how to work within a stack and passing parameters through the stack, as the previous labs before this was simple mathematical equations(ADD, SUB, EOR, etc, and now all of a sudden we get this.)

P.s I also tried using Dissasmebler in AVR Gcc but all it gives me is an infinite loop in the for loop section, as well as me needing to use LDD which was not present.

Any help would be appreciated.

Kindest Regards