0

我正在尝试从标准输入读取字符串并使用 x86、NASM 和系统调用将其打印出来。读入一个字节将是一个函数,而写出一个字节将是一个函数。我正在从标准输入读取字符串并将每个字符放入一个数组中。这是我最初的想法:

;read_writer.asm
section .data
    arr times 100 db 0   ; array of 100 elements initialzed to 0
    ptr dd 0

section .text
global _start
_start:
    push ebp   ; setup stack
    mov ebp, esp   ; setup stack
    push, 0x10   ; allocate space for potential local variables
    call readin: ;call read in func
    push eax  ;return char from readin will be in eax, push it for writeout 
    call writeout:
    leave 
    mov eax, 1 ;exit
    mov ebx, 0
    int 0x80

readin:
   push ebp
   mov ebp, esp ; setup stack

   mov eax, 3 ;read
   mov ebx, 1 ;stdin
              ; i feel like i am missing code here bit not sure what

   leave  ;reset stack
   ret ;return eax

writeout:
   push ebp
   mov ebp, esp ; setup stack
   push eax   ;push eax since it was pushed earlier
   mov eax, 4 ;write
   mov ebx, 1 ;stdout
              ; i feel like i am missing code here bit not sure what

   leave  ;reset stack
   ret ;return eax

样本输入:

Hello World

样本输出:

Hello World

这些函数应该与 cdecl 一起使用,我认为我做得不对。我也意识到我没有将字符放入 arr。

4

1 回答 1

1

对于初学者,您在和int 0x80中都缺少.readinwriteout

而且,正如您在此处看到的那样,两者sys_readsys_write期待(const) char*in ecx。该地址应指向存储要写入的字节/应存储读取的字节的缓冲区。
的值edx应设置为要读取/写入的字节数。

因此,在readin示例中,您需要以下内容:

mov eax, 3    ;read
mov ebx, 0    ;stdin. NOTE: stdin is 0, not 1
sub esp,4     ; Allocate some space on the stack
mov ecx,esp   ; Read characters to the stack
mov edx,1
int 0x80
movzx eax,byte [esp]  ; Place the character in eax, which is used for function return values
add esp,4

同样对于writeout.

于 2015-04-15T06:57:46.713 回答