用户输入和输出在我的汇编代码中不起作用

Str*_*lok 3 macos x86 assembly nasm system-calls

以下程序编译时没有错误,但运行时它不会提示任何输入,也没有打印任何内容.有什么问题,我该如何解决?

我使用这些命令来汇编和链接:

/usr/local/bin/nasm -f macho32 $1
ld -macosx_version_min 10.9.0 -lSystem -o run $filename.o -e _start -lc
Run Code Online (Sandbox Code Playgroud)

我的代码是:

section .data
    ;New line string
    NEWLINE: db 0xa, 0xd
    LENGTH: equ $-NEWLINE

section .bss    
INPT: resd 1

section .text   
global _start
_start:


;Read character
mov eax, 0x3
mov ebx, 0x1
mov ecx, INPT
mov edx, 0x1
int 80h

;print character
mov eax, 0x4
mov ebx, 0x1
mov ecx, INPT
mov edx, 0x1
int 80h

;Print new line after the output 
mov eax, 0x4
mov ebx, 0x1
mov ecx, NEWLINE
mov edx, LENGTH
int 0x80

;Terminate
mov eax, 0x1
xor ebx, ebx
int 0x80
Run Code Online (Sandbox Code Playgroud)

Mic*_*tch 5

您的代码中有迹象表明您在为OS/X(BSD)生成代码时可能一直在使用Linux教程.Linux和OS/X具有不同的SYSCALL调用约定.在OS/X 32位程序中,int 0x80需要在堆栈上传递参数(EAX中的系统调用除外).

在OS/X上通过32位SYSCALL进行操作时需要注意的重要事项int 0x80是:

  • 在栈上传递的参数,从右向左推
  • 在推送所有参数后,必须在堆栈上分配额外的4个字节(DWORD)
  • eax寄存器中的系统调用号
  • 通过中断0x80调用

以相反的顺序在堆栈上推送参数后,int 0x80必须在堆栈上分配额外的4个字节(DWORD).堆栈中该内存位置的值无关紧要.此要求是旧UNIX约定的工件.

可以在APPLE 头文件中找到SYSCALL编号及其参数的列表.你需要这些SYSCALL:

1 AUE_EXIT    ALL { void exit(int rval); }
3 AUE_NULL    ALL { user_ssize_t read(int fd, user_addr_t cbuf, user_size_t nbyte); } 
4 AUE_NULL    ALL { user_ssize_t write(int fd, user_addr_t cbuf, user_size_t nbyte); } 
Run Code Online (Sandbox Code Playgroud)

我已经评论了一些示例代码,这些代码在功能上与您尝试实现的代码类似:

section .data
    ;New line string
    NEWLINE: db 0xa, 0xd
    LENGTH: equ $-NEWLINE

section .bss
    INPT: resd 1

global _start

section .text
_start:
    and     esp, -16      ; Make sure stack is 16 byte aligned at program start
                          ;     not necessary in this example since we don't call 
                          ;     external functions that conform to the OS/X 32-bit ABI

    push    dword 1       ; Read 1 character
    push    dword INPT    ; Input buffer
    push    dword 0       ; Standard input = FD 0
    mov     eax, 3        ; syscall sys_read
    sub     esp, 4        ; Extra 4 bytes on stack needed by int 0x80
    int     0x80
    add     esp, 16       ; Restore stack

    push    dword 1       ; Print 1 character
    push    dword INPT    ; Output buffer = buffer we read characters into
    push    dword 1       ; Standard output = FD 1
    mov     eax, 4        ; syscall sys_write
    sub     esp, 4        ; Extra 4 bytes on stack needed by int 0x80
    int     0x80
    add     esp, 16       ; Restore stack

    push    dword LENGTH  ; Number of characters to write
    push    dword NEWLINE ; Write the data in the NEWLINE string
    push    dword 1       ; Standard output = FD 1
    mov     eax, 4        ; syscall sys_write
    sub     esp, 4        ; Extra 4 bytes on stack needed by int 0x80
    int     0x80
    add     esp, 16       ; Restore stack

    push    dword 0       ; Return value from program = 0
    mov     eax, 1        ; syscall sys_exit
    sub     esp, 4        ; Extra 4 bytes on stack needed by int 0x80
    int     0x80
Run Code Online (Sandbox Code Playgroud)

and esp, -16,如果你需要到堆栈对齐到16字节边界作为未来堆栈操作基线才是必需的.如果您打算调用符合OS/X 32位ABI的外部函数,则堆栈应在函数CALL之前立即对齐16字节.系统调用通过此对齐不是必需的int 0x80.

您应该能够组装并链接它:

nasm -f macho32 test.asm -o test.o
ld -macosx_version_min 10.9.0 -o test test.o -e _start -lSystem
Run Code Online (Sandbox Code Playgroud)

并运行它:

./test
Run Code Online (Sandbox Code Playgroud)