assembly - 使用程序集 8086 搜索数组

标签 assembly x86-16

我正在尝试为类(class)做作业,但我被困在这里了。我需要做的是在双字数组中搜索指定的双字值。这是我现在拥有的:

; 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,即使该值存在于数组中。有人能弄清楚我做错了什么吗?

提前致谢

最佳答案

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] 的值多次。 Soo,您的矩阵从 Adress ebx 开始,内部只有一个值。这个就是第五个输入数字的值!

你的比较很好。问题不在那里。无论如何,我建议你尝试使用迭代!节省时间并减少编程错误! 例如,您可以使用 edx(dl 只是 8 位)作为计数器,因为它在此程序中没有其他用途!

关于assembly - 使用程序集 8086 搜索数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9949950/

相关文章:

c - 8086/386 asm 与 bcc5 : returning long int from asm proc

x86 - 当预取队列中只剩下 1 个字节时,8086 是否启动 1 个字节的代码获取?

assembly - Intel 8086 TASM - 非法号码

assembly - 无法理解用于基本转换的 OR

assembly - 简单 FASM "Hello world!"在 DOS 中断时崩溃

c - 我的 Assembly 实现中的段错误

iphone - 在 iPhone 上编写 ARM 汇编代码的工作流程

c - 在 gcc 内联汇编中加载寄存器? (简单的?)

assembly - NASM - 如何将 8 位寄存器移动到完整的 32 位寄存器?

assembly - 汇编如何在操作系统上工作?