2011-07-30 8 views
2

私は様々な理由でアセンブリを学んでいますが、hello bla bla bla という名前のコードを記述しようとしていますが、あなたの名前を読み取り、あなたの名前を印刷してmore bla bla bla アセンブリコードのご協力

.section .bss 
    .lcomm bufname, 256 
.section .data 
    msg1: 
    .ascii "Hello, please enter your name!\n" # Message to write 
    len1 = . - msg1     # Length of string 

    msg2: 
    .ascii "Good to meet you " 
    len2 = . - msg2 

    msg3: 
    .ascii ". Iam your first assembly program!\n" 
    len3 = . - msg3 

.section .text 

.globl _start 

_start: 
    call get_name 
get_name: 
    #should print msg1 
    mov $4, %eax  # system call number (sys_write) 
    mov $1, %ebx  # file descriptor (stdout) 
    mov $msg1, %ecx  # Message to write 
    mov $len1, %edx  # Lenght of message 
    int $0x80  # Call kernel 

    #should get user input 
    mov $3, %eax  # System call number (sys_read) 
    mov $0, %ebx  # File descriptor (stdin) 
    mov $bufname, %ecx # Buffer to store the name 
    mov $256, %edx  # Lenght of buffer 
    int $0x80 

    #should print msg2  
    mov $4, %eax 
    mov $1, %ebx 
    mov $msg2, %ecx 
    mov $len2, %edx 
    int $0x80 

    #should print bufname (doesn't) 
    mov $bufname, %ecx 
    mov $256, %edx 
    int $0x80 

    #should print msg3 (doesn't) 
    mov $msg3, %ecx 
    mov $len3, %edx 
    int $0x80 

    call exit 
exit: 
    mov $1, %eax 
    mov $0, %ebx 
    int $0x80 

私は

as Jes.s -o Jes.o 
ld Jes.o -o Jes 
を使用してコンパイルするには:それは愚かなその何かが...申し訳ありません...ここ

は、私がこれまで持っているものであることを確認してくださいあなたの名前 イムを取得した後、テキストを表示しません。

これは

Good to meet you renato. Iam your first assembly program! 

なぜこれが間違って表示する必要があり、出力は私が

$ ./Jes 
Hello, please enter your name! 
renato 
Good to meet you 

を取得するのですか? お時間をいただきありがとうございます!

答えて

1

割り込み(int)を呼び出すと、レジスタが上書きされるという問題があります。レジスタが保存され、リストアされる関数呼び出しのようなものではありません。あなたのint通話のすべての前に、これらの行の重複を配置する必要があります:あなたが名前を印刷しているとき

mov $4, %eax  # system call number (sys_write) 
mov $1, %ebx  # file descriptor (stdout) 
+0

Worked = D ありがとう! – Renato

2

、あなたはEAXとEBXは前回SYS_WRITEから変化していないことを前提としています。そうではないかもしれません。

また、名前を書き込むときにバッファの長さを渡しています。名前の長さを過ぎてはいけないと確信していますか? sys_readは、読み込まれたものの長さを返しているはずです。 (msg2を印刷している間はどこかに保存する必要があります)