2017-02-01 7 views
0

私はまだアセンブリを学んでいるので、私の質問は簡単かもしれません。 私はユーザー入力を取得し、次の行に出力として与えるsyscallでエコープログラムを作成しようとしています。X86 64エコープログラムのLinuxシステムコール

section .text 
    global _start 

_start: 
    mov rax,0 
    mov rdx, 13 
    syscall 

    mov rsi, rax 
    mov rdx, 13 
    mov rax, 1  
    syscall 

    mov rax, 60 
    mov rdi, 0 
    syscall 
+6

そして、質問はありますか? – Ped7g

答えて

1

入力ストリームを出力ストリームに戻すことを前提としているため、いくつかのことを行う必要があります。

まず、コードにsection .bssを作成します。これはデータを初期化するためのものです。 label resb sizeInBitsで任意の名前で文字列を初期化します。デモンストレーションのためにはechoと呼ばれる32ビットの文字列になります。

追加メモ、 ';'文字は、//と同じようなコメントに使用されます。

例コード

section .data 
    text db "Please enter something: " ;This is 24 characters long. 

section .bss 
    echo resb 32 ;Reserve 32 bits (4 bytes) into string 

section .text 
    global _start 

_start: 
    call _printText 
    call _getInput 
    call _printInput 

    mov rax, 60 ;Exit code 
    mov rdi, 0 ;Exit with code 0 
    syscall 

_getInput: 
    mov rax, 0 ;Set ID flag to SYS_READ 
    mov rdi, 0 ;Set first argument to standard input 
    ; SYS_READ works as such 
    ;SYS_READ(fileDescriptor, buffer, count) 

    ;File descriptors are: 0 -> standard input, 1 -> standard output, 2 -> standard error 
    ;The buffer is the location of the string to write 
    ;And the count is how long the string is 

    mov rsi, echo ;Store the value of echo in rsi 
    mov rdx, 32 ;Due to echo being 32 bits, set rdx to 32. 
    syscall 
    ret ;Return to _start 

_printText: 
    mov rax, 1 
    mov rdi, 1 
    mov rsi, text ;Set rsi to text so that it can display it. 
    mov rdx, 24 ;The length of text is 24 characters, and 24 bits. 
    syscall 
    ret ;Return to _start 

_printInput: 
    mov rax, 1 
    mov rdi, 1 
    mov rsi, echo ;Set rsi to the value of echo 
    mov rdx, 32 ;Set rdx to 32 because echo reserved 32 bits 
    syscall 
    ret ;Return to _start 
関連する問題