简体   繁体   中英

When popping a number from the stack in aarch64 it doesn't work in cmp

A few days ago I wrote a question about not being able to print numbers without C. Since then I've gotten it to work for the numbers not to be printed in reverse via the stacks FI-LO

The way i do it is that in the beginning I push 10 onto the stack. I do this for 2 reasons:

A: 10 is a double-digit-number, so I know I will never have it on the stack and use it as "last digit" and

B: 10 is ascii newline, so I can use the same printloop to print it too and in the end check if it's 10

Now that I've explained I wanna show the actual code:

.globl _start

_start:
        b printNumberStart

printNumberStart:
        sub sp, sp, #(16*20)
        mov x12, #12345   /*the num to print*/
        mov x16, #10
        str x16, [sp, #-16]!

getStrs:
        udiv x14, x12, x16
        msub x13, x14, x16, x12
        sub x12, x12, x13
        udiv x12, x12, x16
        add x13, x13, #48

        str w13, [sp, #-16]!

        cmp x12, #0
        beq printStrs
        b getStrs

printStrs:
        mov x1, sp
        ldr x3, [sp], #16
        mov x2, #1
        mov w8, #64
        svc #0
        cmp x1, #10
        beq exit
        b printStrs

Because this is my first time using the stack, I included the entire code so maybe the issue is somewhere above.

The output is:

12345
'R�
   C�6RL��d@8�����8�H$pamn=OhaitudrGE/sm=te-MmeS./.nar./jdvl./xfofrom/ce-toma:reeu/krrsmstc.mawtesscemodrm/-1sPnaara.narol.nalaka/korm/omsfas/v/eyf.mofpmsaw:rb/aeopfmatymjeO/damnaiempSegmentation fault

So the newline works alongside with anything else (I replaced the 10 with a 55 to see if i would print a 7 and it does), just the cmp x1, #10 doesn't work.

I also wasnt able to pop the number into a register, so I first read the top value and then I pop it. Probably not a good way, please also correct anything else I did stupidly as this is all self taught.

@user3124812 helped me solve this issue.

When I load the StackPointer into x1, it stores the address, which will be used by the syscall to access the address space.

Which is also why I didn't get the pop to work, since I need the address not the Value.

The solution hence is:

printStrs: 
   mov x1, sp
   ldr x10, [sp], #16 
   mov x2, #1 
   mov w8, #64 
   svc #0 
   cmp x10, #10 
   beq exit 
   b printStrs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM