1

初めて来ました、大学生です。アセンブリ言語を使用して簡単なプログラムを作成しました。そして、ループメソッドを使用して、投稿したプログラムの下で行うこととほぼ同じことを実行できるかどうか疑問に思っています。また、MSNメッセンジャーで話せる人を見つけたいので、すぐに質問できます.(可能であれば)ありがとうございます.


.MODEL small
.STACK 400h

.data


prompt                 db      10,13,'Please enter a 3 digit number, example 100:',10,13,'$'    ;10,13 cause to go to next line
first_digit         db      0d
second_digit            db      0d
third_digit         db      0d
Not_prime               db      10,13,'This number is not prime!',10,13,'$'
prime                   db      10,13,'This number is prime!',10,13,'$'
question                db      10,13,'Do you want to contine Y/N $'
counter                 dw      0d
number                  dw      0d
half                    dw      ?


.code


Start:


      mov ax, @data         ;establish access to the data segment
      mov ds, ax            
      mov number, 0d        

LetsRoll:


        mov dx, offset prompt          ; print the string (please enter a 3 digit...)
        mov ah, 9h                      
        int 21h                         ;execute
                                        ;read FIRST DIGIT
    mov ah, 1d          ;bios code for read a keystroke
    int 21h             ;call bios, it is understood that the ascii code will be returned in al
    mov first_digit, al     ;may as well save a copy
    sub al, 30h         ;Convert code to an actual integer
    cbw             ;CONVERT BYTE TO WORD. This takes whatever number is in al and
                    ;extends it to ax, doubling its size from 8 bits to 16 bits
                    ;The first digit now occupies all of ax as an integer
    mov cx, 100d            ;This is so we can calculate 100*1st digit +10*2nd digit + 3rd digit
    mul cx              ;start to accumulate the 3 digit number in the variable imul cx
                    ;it is understood that the other operand is ax
                    ;AND that the result will use both dx::ax
                    ;but we understand that dx will contain only leading zeros
    add number, ax          ;save
                    ;variable <number> now contains 1st digit * 10
                    ;----------------------------------------------------------------------

                    ;read SECOND DIGIT, multiply by 10 and add in
    mov ah, 1d          ;bios code for read a keystroke
    int 21h             ;call bios, it is understood that the ascii code will be returned in al
    mov second_digit, al        ;may as well save a copy
    sub al, 30h         ;Convert code to an actual integer
    cbw             ;CONVERT BYTE TO WORD. This takes whatever number is in al and
                    ;extends it to ax, boubling its size from 8 bits to 16 bits
                    ;The first digit now occupies all of ax as an integer
    mov cx, 10d         ;continue to accumulate the 3 digit number in the variable
    mul cx              ;it is understood that the other operand is ax, containing first digit
                    ;AND that the result will use both dx::ax
                    ;but we understand that dx will contain only leading zeros. Ignore them
    add number, ax          ;save -- nearly finished
                    ;variable <number> now contains 1st digit * 100 + second digit * 10
                    ;----------------------------------------------------------------------

                    ;read THIRD DIGIT, add it in (no multiplication this time)
    mov ah, 1d          ;bios code for read a keystroke
    int 21h             ;call bios, it is understood that the ascii code will be returned in al
    mov third_digit, al     ;may as well save a copy
    sub al, 30h         ;Convert code to an actual integer
    cbw             ;CONVERT BYTE TO WORD. This takes whatever number is in al and
                    ;extends it to ax, boubling its size from 8 bits to 16 bits
                    ;The first digit now occupies all of ax as an integer
    add number, ax          ;Both my variable number and ax are 16 bits, so equal size
        mov ax, number          ;copy contents of number to ax
        mov cx, 2h          
        div cx              ;Divide by cx
        mov half, ax            ;copy the contents of ax to half
        mov cx, 2h;         
        mov ax, number;         ;copy numbers to ax
        xor dx, dx          ;flush dx
    jmp prime_check         ;jump to prime check

print_question:


        mov dx, offset question     ;print string (do you want to continue Y/N?)
        mov ah, 9h          
        int 21h             ;execute
        mov ah, 1h          
        int 21h             ;execute
        cmp al, 4eh         ;compare 
        je Exit             ;jump to exit
        cmp al, 6eh         ;compare
        je Exit             ;jump to exit 
        cmp al, 59h         ;compare
        je Start            ;jump to start
        cmp al, 79h         ;compare
        je  Start           ;jump to start

prime_check:


        div cx;             ;Divide by cx
        cmp dx, 0h          ;reset the value of dx
        je  print_not_prime     ;jump to not prime 
        xor dx, dx;         ;flush dx
        mov ax, number          ;copy the contents of number to ax
        cmp cx, half            ;compare half with cx
        je print_prime          ;jump to print prime section
        inc cx;             ;increment cx by one
        jmp prime_check         ;repeat the prime check 

print_prime:


        mov dx, offset prime        ;print string (this number is prime!)
        mov ah, 9h          
        int 21h             ;execute
        jmp print_question      ;jumps to question (do you want to continue Y/N?) this is for repeat

print_not_prime:


        mov dx, offset Not_prime    ;print string (this number is not prime!)
        mov ah, 9h          
        int 21h             ;execute
        jmp print_question      ;jumps to question (do you want to continue Y/N?) this is for repeat

Exit:


    mov ah, 4ch             
    int 21h             ;execute exit           

        END Start
4

2 に答える 2

1

...ループを使うということですか?あなたはすでにそうです。2回。

それともLOOP命令を使うということですか?それがあなたの目標なら、気にしないでください。最後に聞いたのは、最近では、LOOP は同等の dec、compare、および branch よりもほとんどのプロセッサで遅いということです。それに加えて、あなたの場合、1と0は特別です(1で割ると常に機能し、ロジックが破棄され、0で割るとエラーが発生します)、LOOPする価値がなくなります。

于 2010-04-24T01:37:15.447 に答える
0

私はアセンブリの専門家ではありませんが、アセンブリでループする方法はfinit(反復)のいずれかだと思います。ループの最大数を定義し、jeで終了するたびに比較する値を増やします。

条件付きループも同じですが、数値をインクリメントする代わりに、事前定義された値を別の値と比較して je で終了するため、目的が達成されるまでループします。

私はあなたのコードを詳しく見ていませんでしたが、私は急いでいたので、これがあなたが言ったことではない場合は申し訳ありません。

編集:もちろん、等しい命令の場合はジャンプを使用する必要はありません。状況に応じて、等しくない場合や他の命令を使用することもできます。

編集 2: 一番基本的なことを忘れていました。無限ループのループの先頭に戻るだけです。あなたはおそらくそれを壊す何かが欲しいと思うでしょうが、それはあまり一般的ではありません:)

于 2010-04-24T01:38:53.943 に答える