2016-10-13 21 views
0

私はLinuxのVMでNASMでx86アセンブリコード、intelフォーマットを使用しています。 プログラムは、スペースで区切って2桁を取り出し、合計を出力することになっています。私はGDBでそれを見てきましたが、結果を印刷するのがまったく拒否されるという点を除いて、エラーや何もなく、全体的にうまく機能します。私はアセンブリコードが新ですので、ここで間違っていることを実際に手がかりにしません。アセンブリプログラムに出力がありません

EDIT:関連性の高いビットのみを含むようにコードを短縮しました。おもう。

;Variables 
section .bss 
    digit  resb 1   

_start: 
    ;Input Prompt 
     ;Code block edited out. 

    ;Reads 1st digit input, checks if the read operation was successfull, 
    ;and stores the value in EAX. 
    call _readDigit 
    cmp edx,0 
    jne _end 
    mov eax,ecx 

    ;Reads 2nd digit input, checks for read success, 
    ;and stores the value in EBX. 
    call _readDigit 
    cmp edx,0 
    jne _end 
    mov ebx,ecx 

    ;Sums EAX and EBX, stores result in ECX, 
    ;and calls the write procedure. 
    call _newLine 
    add eax,ebx 
    mov ecx,eax 
    call _writeSum  

;Prints out the sum of two digits in the format 0X for values 
;below 10, or 1X for values greater than 9. 
_writeSum: 
    push eax 
    push ebx 
    push ecx 
    push edx 

    mov [digit],ecx 
    cmp ecx,9  ;Checks if sum > 9. 
    jg _twoDigits 

;Prints out 0 for the first digit in the result. 
_oneDigit: 
    mov ecx,48 
    mov edx,1 
    mov ebx,STDOUT 
    mov eax,SYS_WRITE 
    int 80h 

    mov ecx,[digit] 
    jmp _lastDigit 

;Prints out 1 for the first digit in the result, 
;and subtracts 10 from ECX. 
_twoDigits: 
    mov ecx,49 
    mov edx,1 
    mov ebx,STDOUT 
    mov eax,SYS_WRITE 
    int 80h 

    mov ecx,[digit] 
    sub ecx,10 

;Converts ECX to ASCII and prints this as the 
;second digit in the result. 
_lastDigit: 
    add ecx,'0' 
    mov [digit],ecx 

    mov ecx,[digit] 
    mov edx,1 
    mov ebx,STDOUT 
    mov eax,SYS_WRITE 
    int 80h 

    pop edx 
    pop ecx 
    pop ebx 
    pop eax 
    ret 
+0

strace( 'strace。/ a.out')の下でコードを実行して、実際にシステムコールに渡す引数を確認してください。ところで、あなたのコードはかなり長いですが、それは[mcve]の最小限の部分を本当に満足させるものではありません。参考:[x86タグのwiki](http://stackoverflow.com/tags/x86/info)を参照してください。 –

+1

最初に、 'digit resb 1'が何をしているのか、' mov ecx、[digit] 'が何をするのか説明してください(無効な組み合わせです)。しかし、その部分が誤って動作する可能性があります(作業の煩雑さを避けるため、コードの先頭に 'mov [digit]、dword 0xDEADBEEF'を実行してください)。次に、 'SYS_WRITE'の使用方法を見てください。msgを表示すると、それを表示します。合計を表示するには、別の方法で使用します(それはうまくいきません)。これらの 'int 80h'呼び出しの引数を注意深く見てください。 – Ped7g

答えて

0

私がすぐに見ることは、印刷したい値(48,49、[digit])をecxに直接移動していることです。 SYS_WRITEは、ecxがPOINTERを直接値ではなくテキスト文字列に含めることを想定しています。

第2に、文字列(実際にはすべての入力)に期待する値を手動で入力することを考慮する必要があります。これは、問題が読み取りなどではないことを確かめることができます。最小のエラーの場合には、ほとんどの場合にのみ真となる仮定を作成します。

関連する問題