2

整数値を比較し、どちらの値が大きいかを示す適切なプロンプトを出力する必要があります。

以下に作成したコードは、'i' と 'j' をそれぞれ 5 と 4 に初期化します。目的は、即時値自体ではなく「変数」を比較することです。したがって、この例では、5 と 4 ではなく、「i」と「j」を cmp に入れています。

global _main
extern _printf, _system 

section .text


_main:
; clear screen
    push clr
    call _system
    add esp, 4

;Test prints out i. Successfully does the job.
    push dword [i]          ; why did it work here and not in *cmp*?
    push prompt2
    call _printf      
    add esp, 8

;compare values and do a conditional jump.
    CMP dword [i], dword [j]            ; compares values stored in i and j. i and j are base 10 nums. This is where the error appears.
    JG igreat                           ; jumps if i is greater than j.
    JL jgreat                           ; jumps if j is greater than i.
    JE eks                              ; jumps if i and j are equal.
    ret                                 ; as a measure, this ends the code.

    igreat:                             ; prints out a prompt if i is greater than j.
    push dword [j]
    push dword [i]
    push ibig
    call _printf
    add esp, 16
    ret

    jgreat:                             ; prints out a prompt if j is greater than i.
    push dword [i]
    push dword [j]
    push jbig
    call _printf
    add esp, 16
    ret

    eks:                                ; prints out a prompt if i is equal to j.
    push dword [i]
    push dword [j]
    push ex
    call _printf
    add esp, 16
    ret



last:                                   ; terminates the code
ret

section .data
clr         db "cls",0              
i           dd 5                        ; i is initialized to 5
j           dd 4                        ; j is initialized to 4
prompt2     db "Value is %d",13,10,0
ibig        db "Test 1. Comparisons: %d is bigger than %d",13,10,0
jbig        db "Test 2. Comparisons: %d is bigger than %d",13,10,0
ex          db "Test 3. Comparisons: %d is bigger than %d",13,10,0

私が提起するかもしれない2つの質問:

  1. CMP dword [i]、dword [j]でエラーが発生します。オペコードとオペランドの組み合わせが無効です。しかし、以前の呼び出し/コード行で printf 関数を使用して値を正常に回復しました。何故ですか?
  2. dword [j]を即時の、たとえば CMP dword [i]、9 に置き換えようとしました。正しいプロンプトが出力されますが、プログラムが応答しなくなります。何故ですか?

私は Windows 8 Intel 32 ビットを実行していることを思い出してください。このコードは、DoSBox で実行される NASM と CMD の GCC で「コンパイル」されています。

私はまったくの初心者であり、どんな助けでも大歓迎です。ありがとうございました!

4

1 に答える 1