2012-03-30 18 views
2

私はクラスの割り当てをしようとしています。私がする必要があるのは、指定されたダブルワード値のダブルワードの配列を検索することです。ここで私は今持っているものです。アセンブリを使用して配列を検索する8086

; DriverSub assembly language program: SUB adds two numbers pushed by Driver and displays SUM 
; Author: Daniel Strien 
; Using Code from: RSzabo 
; Date: 3/29/2012 



.386 
.MODEL FLAT 

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD 



INCLUDE io.h   ; header file for input/output 

cr  EQU  0dh  ; carriage return character 
Lf  EQU  0ah  ; line feed 


.STACK 4096   ; reserve 4096-byte stack 


.DATA     ; reserve storage for data 

number12 DWORD ? 
array DWORD 5 DUP (?) 
prompt1 BYTE "Enter first number: ", 0 
prompt2 BYTE "Enter another number: ", 0 
prompt3 BYTE "Enter a number to search for: ", 0 
string BYTE 13 DUP (?) 

label1 BYTE cr, Lf, "The value was found at Position (0 if not found): "  
pos  BYTE 16 DUP (?) 

     BYTE cr, Lf, 0 




.CODE       ; start of main program code 

_start: 

lea ebx, array  ; get address of array 

    output prompt1   ; prompt for first number 
    input string,13  ; read ASCII characters 
    atod string   ; convert to integer 
    mov [ebx], eax  ;move the input to the array 

output prompt2   ; repeat for second number 
    input string, 13 
    atod string 
    mov [ebx], eax  ;move the input to the array 

output prompt2   ; repeat for third number 
    input string, 13 
    atod string 
    mov [ebx], eax  ;move the input to the array 

output prompt2   ; repeat for fourth number 
    input string, 13 
    atod string 
    mov [ebx], eax  ;move the input to the array 

output prompt2   ; repeat for fifth(last) number 
    input string, 13 
    atod string 
    mov [ebx], eax  ;move the input to the array 

push ebx ;pushing the array adress to the stack 

output prompt3  ; get search number 
input string, 13 
atod string 
push eax 


    lea eax, number12 
    push eax 
    call search 

    dtoa pos, number12  ; convert to ASCII characters 
    output label1   ; output label and position 

    INVOKE ExitProcess, 0 ; exit with return code 0 


PUBLIC _start     ; make entry point public 



search  PROC NEAR32 ; add two words passed on the stack 
        ; return the sum in the EAX register 
     push ebp    ; save EBP 
     mov ebp,esp   ; establish stack frame 

;move search value to eax 
mov eax, [ebp+12] 


     ;compare values to the user input 
    mov ebx, [ebp+16]  ; move array adress to EBX 
    mov ecx, [ebx]  ;move first element to ECX 
    cmp ecx, eax  ;comparing search number to the first value in the array 
    je first    ;If equal return the position. 

    mov ecx, [ebx+4]  ;move first element to ECX 
    cmp ecx, eax  ;comparing search number to the second value in the array 
    je second   ;If equal return the position. 

    mov ecx, [ebx+8] 
    cmp ecx, eax ;comparing search number to the third value in the array 
    je third    ;If equal return the position. 

    mov ecx, [ebx+12] 
    cmp ecx, eax  ;comparing search number to the fourth value in the array 
    je fourth    ;If equal return the position. 

    mov ecx, [ebx+16] 
    cmp ecx, eax  ;comparing search number to the fifth value in the array 
    je fifth    ;If equal return the position. 
    jmp none 

first:     ;returns position 1 
    mov eax, 1  
    jmp done 

second:    ;returns position 2 
    mov eax, 2 
    jmp done 

third:     ;returns position 3 
    mov eax, 3 
    jmp done 

fourth:    ;returns position 4 
    mov eax, 4 
    jmp done 

fifth:     ;returns position 5 
    mov eax, 5 
    jmp done 

none:     ;returns 0 if the search value is not found. 
    mov eax, 0 
    jmp done 

done: 
    mov ebx,[ebp+8] 
    mov [ebp],eax 

     pop ebp    ; restore EBP 
     ret 12     ; return 

search  ENDP 
END        ; end of source code 

私がいる問題は、配列に値が存在していても、プログラムはすべてのものを介して実行それでも0を返すということです。私が間違っていることを誰でも理解できますか?事前に

おかげ

+1

「宿題」タグが必要ですか? –

+0

タグが追加されました。ありがとうございます。 – user1304088

+0

'_start'セクションでは、すべての' mov [ebx]、eax'の後に 'ebx'を増やさないので、すべての入力データを最初のdwordに置きます(これは予約しているので問題ありません)それのための5 DWORD)。 これはうんざりですが、スタックをRETアドレスと共に使用している可能性があります。 'array'は定数ですが、スタックのアドレスをスタックに/からポップ/ポップしないで、そのアドレスをどこでも直接使用します(可能性のあるエラーを排除して後で追加できます)。 (私は2つのパラメータを見た)レジスタを介して '検索 '。 – karatedog

答えて

2
lea ebx, array  ; get address of array 

output prompt1   ; prompt for first number 
input string,13  ; read ASCII characters 
atod string   ; convert to integer 
mov [ebx], eax  ;move the input to the array 

output prompt2   ; repeat for second number 
input string, 13 
atod string 
mov [ebx], eax  ;move the input to the array 

....

あなたがすべてでEBXをインクリメントしていなかったようです。行列に別の値を置くたびにebxを4でインクリメントするのを忘れてしまったら、[ebx]の値をあなたの入力で数回オーバーライドしたことになります。 Ado ebxで始まるあなたの行列Sooは、内部に1つの値しか持たないでしょう。これは5番目の入力番号の値です!

あなたの比較は良いです。そこに問題はありません。とにかく、反復を代わりに使用することをお勧めします!時間を節約し、プログラミングエラーを減らします! このプログラムで他の目的がない限り、例えばedx(ちょうど8ビット)をカウンターとして使うことができます!

関連する問題