位移后,汇编没有返回我想要的内容

问题描述 投票:0回答:1

免责声明:我是初学者,这是我见过的最糟糕的语言

目标是解码消息。 每个字符都编码为 8 个字节

  • 7-8字节无关紧要,假设0
  • 3-6字节索引
  • 第二个字节量(需要打印字符的次数)
  • 第一个字节字符

字符和金额是正确的,但由于某种原因我无法获得正确的索引。 通过索引,我可以转到需要从 $message 打印的字符的下一个地址

这不仅仅是一些分段错误,所以我真的不明白为什么我没有得到正确的索引值。现在我得到: “呵呵
l!olred”而不是“Hello world!”

decode:
pushq    %rbp
movq     %rsp, %rbp

pushq    %rdi 
pushq    $4 # 16 Stack allignment

movq    (%rdi), %rax     # Load 8-byte message from address in %rdi

movb    %al, %sil        # Move the least significant byte of %rax to %sil
shr     $8, %rax         # Shift %rax right by 8 bits
movb    %al, %bl         # Move the least significant byte of %rax to %bl
shr     $8, %rax         # Shift %rax right by 8 bits

.loop:
cmpl   $0, %eax
je     .finish

movq    $String, %rdi    # Move the format string to %rdi
call    printf           # Print the character

dec     %bl              # Decrement the "amount" counter
cmpb    $1, %bl          # Compare %bl (amount) with 1 
jg      .loop              

popq    %rdi
popq    %rdi            # Remove 16 bytes

imull   $8, %eax         # Multiply the index by 8 (8 byte memory blocks)
addq    %rax, %rdi

call    decode

.finish: 
movq %rbp, %rsp
popq %rbp
ret


main:
pushq   %rbp             # Push the base pointer (and align the stack)
movq    %rsp, %rbp       # Copy stack pointer value to base pointer

movq    $MESSAGE, %rdi   # First parameter: address of the message
call    decode           # Call decode

popq    %rbp             # Restore base pointer location 
movq    $0, %rdi         # Load program exit code
call    exit             # Exit the program
assembly x86-64 att
1个回答
0
投票

每个字符索引是一个字节= 8 位。 将字节索引增加一个字节,而不是加倍 8。

© www.soinside.com 2019 - 2024. All rights reserved.