Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos NASM 64位OS X输入字符串覆盖现有值的字节_Macos_Assembly_X86_Nasm_X86 64 - Fatal编程技术网

Macos NASM 64位OS X输入字符串覆盖现有值的字节

Macos NASM 64位OS X输入字符串覆盖现有值的字节,macos,assembly,x86,nasm,x86-64,Macos,Assembly,X86,Nasm,X86 64,我正试图编写一个简单的汇编程序,将两个数字相加。我希望用户能够输入值。我遇到的问题是,当我显示一条字符串消息,然后在中读取一个值时,下次需要该字符串时,该字符串的前x个字符已被用户输入的数据覆盖 我的假设是,这与使用LEA将字符串加载到寄存器有关。我这样做是因为Macho64会抱怨在这种情况下是否使用了常规的MOV指令(与Mac上64位的寻址空间有关) 我的代码如下: section .data ;this is whe

我正试图编写一个简单的汇编程序,将两个数字相加。我希望用户能够输入值。我遇到的问题是,当我显示一条字符串消息,然后在中读取一个值时,下次需要该字符串时,该字符串的前x个字符已被用户输入的数据覆盖

我的假设是,这与使用
LEA
将字符串加载到寄存器有关。我这样做是因为Macho64会抱怨在这种情况下是否使用了常规的
MOV
指令(与Mac上64位的寻址空间有关)

我的代码如下:

    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
程序循环两次。我第一次得到以下提示:

请输入您的下一个号码:

我将输入类似于
5
(后面是返回键)的内容

下一个提示是:

5
请输入您的下一个号码:


任何帮助都将不胜感激。

我认为Mac上的所有64位代码都必须是rip相关的。 不支持绝对地址。在这种类型的寻址中,您可以相对于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

您还可以看到。

看起来您试图对提示和输入数据使用相同的缓冲区-因此在第一次输入“5”后,您已经覆盖了提示的前两个字符。尝试使用单独的输入缓冲区进行输入。我不完全确定您的意思-寄存器
RSI
是否不用于为系统写入提供输出,以及在系统读取后输入存储在何处?我不知道这个可以定制?我确实考虑过尝试将
请在另一个寄存器中输入
字符串(比如
R10
),但由于使用
LEA
加载字符串的方式,因此一旦将其移动到
RSI
,同样的问题就会发生(因为它将字符串的地址存储到内存中,而不是存储在寄存器中的值)。有没有一种方法可以在不使用
LEA
的情况下使用/复制字符串?Mac使用相对寻址,在AT&T语法中,您必须使用类似
LEA str(%rip),%rsi
。(str是我的字符串标签,%rsi是我的目的地。)因此对于我的代码,它类似于:
lea%rsi,input_message(%rip)
?但我不太清楚为什么会涉及指令指针?
rsi
在写入和后续读取时都指向同一缓冲区(
input\u message
)。您需要第二个缓冲区进行读取(称之为
input\u buffer
),然后在读取之前将
rsi
指向第二个缓冲区。这样,您读取的字符将不会覆盖存储在
input\u message
中的提示。请参阅上的OSX示例。