So I'm working on a simple game just to teach myself linux x64, and I'm trying to move a value I got from a read call into a parameter in the .data section
My code is as follows (the important stuff is near the bottom):
max_msg_len:
.int 30
input:
.zero 30
action_msg:
.asciz "what will you do? 1. purchase 2. sell 3. haggle\n"
.equ action_msg_len, . - action_msg
temp_msg:
.ascii "Value is: "
.equ temp_msg_len, . - temp_msg
newline: # so I can write just a new line
.byte 10
action:
.byte 0
amount:
.int 0
money:
.int 20
score:
.int 0
life:
.int 1
.text
.globl main
main:
get_input:
#write(STDOUT, action_msg, action_msg_len)
mov $action_msg_len, %rdx
mov $action_msg, %rsi
mov $1, %rdi
mov $1, %rax
syscall
#read(STDIN_FILENO, input, max_msg_len)
mov $max_msg_len, %rdx
mov $input, %rsi
mov $0, %rdi
mov $0, %rax
syscall
#write(STDOUT_FILENO, temp_msg, temp_msg_len)
mov $temp_msg_len, %rdx
mov $temp_msg, %rsi
mov $1, %rdi
mov $1, %rax
syscall
#write(STDOUT_FILENO, action, max_ms)
mov $1, %rdx
mov $action, %rsi
mov $1, %rdi
mov $1, %rax
syscall
#write(STDOUT_FILENO, newline, 1)
mov $1, %rdx
mov $newline, %rsi
mov $1, %rdi
mov $1, %rax
syscall
# these are the offending lines
movb $input, $action
/* I tried to get it to work with registers first, this also doesn't work
movb $action, %al
movb $input, %bl */
action_1:
#purchase
action_2:
#sell
action_3:
#haggle
loop:
# if life == 0
# jmp to end
# else
# jmp to get_input
end:
#exit(0)
mov $0, %rdi
mov $60, %rax
syscall
Essentially what I want to do is move the first byte of input to the action parameter so that I can then do a comparison. and jump to the different actions.
When I assemble the code above as is (with gcc -no-pie
) I get
crack_self.s: Assembler messages:
crack_self.s:65: Error: unsupported instruction `mov'
When I try to put the values into a register first, see the comment, I get the following error instead
/tmp/cceFdz3A.o: in function `get_input':
(.text+0x97): relocation truncated to fit: R_X86_64_8 against `.data'
(.text+0x99): relocation truncated to fit: R_X86_64_8 against `.data'
collect2: error: ld returned 1 exit status
I'm not sure if this error means I'm simply not allowed to do this, or if there's some issue with the size of the things I'm moving, or the fact that input is actually 30 bytes and I'm just trying to get the first one.
What is the proper way to do this? I can't find good documentation or examples on the topic. I just find bits and pieces on different pages that don't give me the full picture
Thanks