NASM 64 位 OS X 输入的字符串覆盖现有值的字节
NASM 64-bit OS X Inputted String Overwriting Bytes of Existing Value
我正在尝试编写一个简单的汇编程序来将两个数字相加。我希望用户能够输入值。我遇到的问题是,当我显示一个字符串消息然后读入一个值时,下次需要该字符串时,该字符串的前 x 个字符已被用户输入的数据覆盖。
我的假设是,这与使用 LEA
将字符串加载到寄存器有关。我一直在这样做,因为 Macho64 抱怨如果在这种情况下使用常规 MOV
指令(与在 Mac 上以 64 位寻址 space 有关) .
我的代码如下:
section .data ;this is where constants go
input_message db 'Please enter your next number: '
length equ $-input_message
section .text ;declaring our .text segment
global _main ;telling where program execution should start
_main: ;this is where code starts getting executed
mov r8, 0
_loop_values:
call _get_value
call _write
inc r8 ;increment the loop counter
cmp r8, 2 ;compare loop counter to zero
jne _loop_values
call _exit
_get_value:
lea rcx, [rel input_message] ;move the input message into rcx for function call
mov rdx, length ;load the length of the message for function call
call _write
call _read
ret
_read:
mov rdx, 255 ;set buffer size for input
mov rdi, 0 ;stdout
mov rax, SYSCALL_READ
syscall
mov rdx, rax ;move the length from rax to rdx
dec rdx ;remove new line character from input length
mov rcx, rsi ;move the value input from rsi to rcx
ret
_write:
mov rsi, rcx ;load the output message
;mov rdx, rax
mov rax, SYSCALL_WRITE
syscall
ret
_exit:
mov rax, SYSCALL_EXIT
mov rdi, 0
syscall
程序按预期循环两次。第一次得到如下提示:
Please enter your next number:
我会输入类似 5
的内容(后跟 return 键)
下一个提示是:
5
ease enter your next number:
如有任何帮助,我们将不胜感激。
我认为 Mac 上的所有 64 位代码都必须是相对的。
不支持绝对地址。在这种类型的寻址中,您可以相对于 rip 寻址您的符号。
NASM 文档说:
default abs
mov eax,[foo] ; 32−bit absolute disp, sign−extended
mov eax,[a32 foo] ; 32−bit absolute disp, zero−extended
mov eax,[qword foo] ; 64−bit absolute disp
default rel
mov eax,[foo] ; 32−bit relative disp
mov eax,[a32 foo] ; d:o, address truncated to 32 bits(!)
mov eax,[qword foo] ; error
mov eax,[abs qword foo] ; 64−bit absolute disp
你还可以看到this question。
我正在尝试编写一个简单的汇编程序来将两个数字相加。我希望用户能够输入值。我遇到的问题是,当我显示一个字符串消息然后读入一个值时,下次需要该字符串时,该字符串的前 x 个字符已被用户输入的数据覆盖。
我的假设是,这与使用 LEA
将字符串加载到寄存器有关。我一直在这样做,因为 Macho64 抱怨如果在这种情况下使用常规 MOV
指令(与在 Mac 上以 64 位寻址 space 有关) .
我的代码如下:
section .data ;this is where constants go
input_message db 'Please enter your next number: '
length equ $-input_message
section .text ;declaring our .text segment
global _main ;telling where program execution should start
_main: ;this is where code starts getting executed
mov r8, 0
_loop_values:
call _get_value
call _write
inc r8 ;increment the loop counter
cmp r8, 2 ;compare loop counter to zero
jne _loop_values
call _exit
_get_value:
lea rcx, [rel input_message] ;move the input message into rcx for function call
mov rdx, length ;load the length of the message for function call
call _write
call _read
ret
_read:
mov rdx, 255 ;set buffer size for input
mov rdi, 0 ;stdout
mov rax, SYSCALL_READ
syscall
mov rdx, rax ;move the length from rax to rdx
dec rdx ;remove new line character from input length
mov rcx, rsi ;move the value input from rsi to rcx
ret
_write:
mov rsi, rcx ;load the output message
;mov rdx, rax
mov rax, SYSCALL_WRITE
syscall
ret
_exit:
mov rax, SYSCALL_EXIT
mov rdi, 0
syscall
程序按预期循环两次。第一次得到如下提示:
Please enter your next number:
我会输入类似 5
的内容(后跟 return 键)
下一个提示是:
5
ease enter your next number:
如有任何帮助,我们将不胜感激。
我认为 Mac 上的所有 64 位代码都必须是相对的。
不支持绝对地址。在这种类型的寻址中,您可以相对于 rip 寻址您的符号。
NASM 文档说:
default abs
mov eax,[foo] ; 32−bit absolute disp, sign−extended
mov eax,[a32 foo] ; 32−bit absolute disp, zero−extended
mov eax,[qword foo] ; 64−bit absolute disp
default rel
mov eax,[foo] ; 32−bit relative disp
mov eax,[a32 foo] ; d:o, address truncated to 32 bits(!)
mov eax,[qword foo] ; error
mov eax,[abs qword foo] ; 64−bit absolute disp
你还可以看到this question。