我完全是汇编语言的新手。我试图制作简单的程序来破译凯撒密码。问题是链接后我得到这些错误:
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
答案 0 :(得分:4)
错误是指你写的两行:
add $key, %bh
这会尝试将key
的值(即字符串-128
的地址)添加到bh
。由于地址大于255,因此它不适合像bh
这样的字节寄存器。因此,您会收到链接器指示的错误。
我没有通过将key
的地址加载到bh
来分析您尝试执行的操作,但这样做并不起作用。如果要在key
加载数据的第一个字节,请写入
add key(%rip), %bh
如果要加载key
的地址,则需要使用lea
将其加载到64位寄存器中,如下所示:
lea key(%rip), %rbx