user7554810
user7554810

Reputation:

Why is Assembly printing a junk char from a return value if I print something first?

.text
.global main
main:    
    push %r13
    push %r14
    push %r15
    jmp label
x:
    pop %rbx
    movq $0, %rax
    movq $1, %r9
    pushq %r9
    popq %rsi
    movq %rsi, %r15
    .data
    a1:    .quad   0
    .text
    mov %r15,a1
    mov $format,%rdi
    movq $5, %r9
    pushq %r9
    popq %rsi
    movq %rsi, %r15
    call printf
    push a1
    popq %rsi
    movq %rsi, %rax
    push %rbx
    ret
label:
    mov $format,%rdi
    call x
    push %rax
    popq %rsi
    movq %rsi, %r15
    call printf
    mov $0,%rax
    pop %r15
    pop %r14
    pop %r13
    ret
.data
format:    .string "%lu\n"

The expected output is

5

1

but I get

5

The reason I say that it's the first print messing it up is that when I remove the print 5, 1 is correctly printed. I'm unsure why this would mess up the 1 when they are both printed using the same code.

Upvotes: 0

Views: 90

Answers (1)

user7554810
user7554810

Reputation:

I fixed the problem by moving the format string to %rdi after calling label x.

Upvotes: 2

Related Questions