-2

I need to create a simple function that calculates the surface of a rectangle(a simple a*b program). However the value of registers(including R0 and R1) have to remain intact after the function is called. My question is that do I have to use commands POP and PUSH?

start:
.include "m8def.inc"
.DEF var1= R16
.DEF var2= R17
.DEF surface= R18
.DEF temp= R19
rjmp main

surfacerectangle:
pop R20
pop R21
pop temp
add surface, var1
mul surface, var2
push temp
push R21
push R20
ret

main:

ldi temp, low(RAMEND) 
out SPL, temp
ldi temp, high(RAMEND)
out SPH, temp
ldi var1, 3
ldi var2, 4
ldi surface, 0
push temp

rcall surfacerectangle
pop temp

end3:
 nop

Here is my code that I made, however it dose not seem to work. Does anyone see the problem in my code? This was written in Atmel Studio 7.0 for ATmega8 microcontroller.

ile123
  • 57
  • 1
  • 6
  • 3
    “does not seem to work” is not an error description. Please post a description of what your code is supposed to do, what result you expect and what you get instead. Ideally, make a [mcve]. – fuz May 26 '20 at 16:53
  • How can you tell it's not working? There is no output. Also, your code runs once then executes a NOP... and then what happens? The program counter does not stop with a NOP instruction. It just keeps going.That's not the proper way to program a microcontroller. – TomServo May 27 '20 at 00:32

1 Answers1

0
start:
.DEF var1= R16
.DEF var2= R17
.DEF temp= R18
rjmp main

surfacerectangle:
mul var1, var2
ret

main:
ldi temp, low(RAMEND) 
out SPL, temp
ldi temp, high(RAMEND)
out SPH, temp
ldi var1, 3
ldi var2, 4

rcall surfacerectangle
end3:
 nop

Found the problem, turns out the wrong usage of POP and PUSH was cause of the problem(the problem was infinite looping).

ile123
  • 57
  • 1
  • 6