gpt4 book ai didi

assembly - 汇编程序中的重定位截断错误

转载 作者:行者123 更新时间:2023-12-02 20:48:59 24 4
gpt4 key购买 nike

我对汇编语言完全陌生。我尝试编写简单的程序来破译凯撒密码。问题是链接后我收到这些错误:

cezar.o: In function `loop':
(.text+0xbf): relocation truncated to fit: R_X86_64_8 against `.data'
cezar.o: In function `check_letter':
(.text+0xd5): relocation truncated to fit: R_X86_64_8 against `.data'

老实说 - 我不知道这意味着什么。有谁能够帮助我?相反,#explainMeLikeI'm5 ;P

如果需要我的代码:

.data
STDIN = 0
STDOUT = 1
SYSWRITE = 0
SYSREAD = 1
SYSEXIT = 60
EXIT_SUCCESS = 0
BUFLEN = 512
BASE = 10
BASE_NUM = 10
BASE_CHAR = 26
NUM_BEG = 48

error_msg: .ascii "Bad characters in key\n"
error_msg_len = .-error_msg

key: .ascii "-128"
key_len = .-key

.bss
.comm textin, 512
#.comm keyin, 512
.comm textout, 512

.text
.globl _start

_start:
#check if key is good
movq $0, %rdi
movb key(, %rdi, 1), %bl #load first char to bl
cmp $'-', %bl #check if it's -
jne error
movq $1, %rdi #counter
movb key(, %rdi, 1), %bl
cmp $'0', %bl
jl error
cmp $'9', %bl
jg error

mov $key_len, %rdi
mov $1, %r11 #powers of 10
mov $0, %r8 #calculated key goes to r8
sub $1, %rdi #because I want numbers only, "-" doesn't interest me now

ascii_to_num:
cmp $1, %rdi #at rdi=0 jump out of loop
jl load_text

mov $0, %rax #clearing rax
mov key(, %rdi, 1), %al
sub $NUM_BEG, %al
mul %r11 #multiplying al by power of it's position
add %rax, %r8

mov %r11, %rax
mov $10, %r12
mul %r12
mov %rax, %r11

dec %rdi #decrementation
jmp ascii_to_num


load_text:
movq $SYSREAD, %rax
movq $STDIN, %rdi
movq $textin, %rsi
movq $BUFLEN, %rdx

dec %rax #delete '\n'
movq %rax, %r10

movq $0, %rdi #counter
loop:
movb textin(, %rdi, 1), %bh
#check if number
movb $'0', %bl #I'm using bl for comparing
cmp %bl, %bh
jl ending_loop
movb $'9', %bl
cmp %bl, %bh
jg check_letter
add $key, %bh
cmp $'0', %bh
jl correct_num
jmp ending_loop

check_letter:
movb $'A', %bl
cmp %bl, %bh
jl ending_loop
movb $'Z', %bl
cmp %bl, %bh
jg ending_loop
add $key, %bh
cmp $'A', %bh
jl correct_char
jmp ending_loop

correct_num:
add $BASE_NUM, %bh
cmp $'0', %bh
jl correct_num
jmp ending_loop

correct_char:
add $BASE_CHAR, %bh
cmp $'A', %bh
jl correct_char
jmp ending_loop

ending_loop:
movb %bh, textout(, %rdi, 1)
inc %rdi
cmp %r10, %rdi
jle loop
jmp show

error: #error message
mov $SYSWRITE, %rax
mov $STDOUT, %rdi
mov $error_msg, %rsi
mov $error_msg_len, %rdx
syscall

show: #display message and close programm
add $'\n', textout
mov $SYSWRITE, %rax
mov $STDOUT, %rdi
mov $textout, %rsi
mov $BUFLEN, %rdx
syscall

mov $SYSEXIT, %rax
mov $EXIT_SUCCESS, %rdi
syscall

最佳答案

错误指的是您编写的两行:

add $key, %bh

这会尝试将key的值(即字符串-128的地址)添加到bh。由于地址大于 255,因此无法放入 bh 等字节寄存器中。因此,您会收到链接器指示的错误。

我还没有分析过你尝试将key的地址加载到bh中来做什么,但这样做是行不通的。如果要加载key处数据的第一个字节,请写入

add key(%rip), %bh

如果你想加载key的地址,你需要使用lea将其加载到64位寄存器中,如下所示:

lea key(%rip), %rbx

关于assembly - 汇编程序中的重定位截断错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43021855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com