0

stdin から文字列を読み取り、x86、NASM、および Syscalls を使用して出力しようとしています。バイトの読み取りは関数になり、バイトの書き込みは関数になります。stdin から文字列を読み取り、各文字を配列に入れています。これが私の最初のアイデアです:

;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まず、 と の両方readinにがありませんwriteout

そして、ここでわかるように、 との両方sys_readにinsys_writeが期待されます。そのアドレスは、書き込むバイトが格納されている/読み取られたバイトが格納されているバッファを指している必要があります。の値は、読み書きするバイト数に設定する必要があります。(const) char*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 に答える