Print the Stack contents - ARM Assembly

Thread Starter

Ab Abrams

Joined Apr 27, 2017
25
Hi guys,

I was wondering how I can print to the console the contents of the Stack?
Lets say for example that I have put into the R2 the value of 5 and then push the R2 to the stack
Is there any way by calling the printf function or by making a syscall to print the value of '5' by using the SP?

MOV R2, #5
PUSH {R2}

After those commands the SP points at the address where the 5 has been pushed right? How I can use the SP(or the memory address location that points ) as a variable for a printf function?

Any thoughts or ideas how I can implement a scenario like this?

Thanks!
 

Thread Starter

Ab Abrams

Joined Apr 27, 2017
25
You already asked a similar question two months ago. Did the answers then not help you?

https://forum.allaboutcircuits.com/threads/display-registers-value-arm-assembly.144208/

Hi, well not at all, because the answers based on use of GNU Debugger or like yours to use and IDE and just see the values. I want to create a code that prints the values of the Stack, not just use a Debugger.
Also, I dont want to do this on a microcontroller but on RPI or any other Cortex- A model.
I am just asking If anyone knows the commands in Assembly so I can use the "printf" function and through the SP print the contents.
I am pretty clear with What I am looking for.
I am talking to Assembly programmers IF there are out there
 

MrChips

Joined Oct 2, 2009
30,802
Two thoughts come to mind.
Firstly, there is nothing special about the stack. It is just another area of memory and can be accessed like any other memory location.
Secondly, if you are looking for an ASM solution, "printf" is not going to do it.
 

miniwinwm

Joined Feb 2, 2018
68
Hi guys,

I was wondering how I can print to the console the contents of the Stack?
Lets say for example that I have put into the R2 the value of 5 and then push the R2 to the stack
Is there any way by calling the printf function or by making a syscall to print the value of '5' by using the SP?

MOV R2, #5
PUSH {R2}

After those commands the SP points at the address where the 5 has been pushed right? How I can use the SP(or the memory address location that points ) as a variable for a printf function?

Any thoughts or ideas how I can implement a scenario like this?

Thanks!
This...
Code:
    volatile int stackptr;

    asm("LDR r0, =stackptr");
    asm("MOV r1, sp");
    asm("STR r1, [r0]");
...will get the stack pointer into a C variable on an ARM using GCC, but will stuff anything in R0 or R1.

This will preserve R0 and R1, but the value in stackptr will now be 8 bytes out...
Code:
    asm("PUSH {r0}");
    asm("PUSH {r1}");
    asm("LDR r0, =stackptr");
    asm("MOV r1, sp");
    asm("STR r1, [r0]");
    asm("POP {r1}");
    asm("POP {r0}");
 

Thread Starter

Ab Abrams

Joined Apr 27, 2017
25
This...
Code:
    volatile int stackptr;

    asm("LDR r0, =stackptr");
    asm("MOV r1, sp");
    asm("STR r1, [r0]");
...will get the stack pointer into a C variable on an ARM using GCC, but will stuff anything in R0 or R1.

This will preserve R0 and R1, but the value in stackptr will now be 8 bytes out...
Code:
    asm("PUSH {r0}");
    asm("PUSH {r1}");
    asm("LDR r0, =stackptr");
    asm("MOV r1, sp");
    asm("STR r1, [r0]");
    asm("POP {r1}");
    asm("POP {r0}");

Hi @miniwinwm, many many thanks for your answer and reply. Finally a very interesting and helpful answer. Thanks
 
Top