2012-03-03 72 views
1

我试图做一个程序,将数字输入到一个数组,然后能够搜索数组中的其中一个数字。我可以得到它来阅读和搜索好,找到号码,但用户应该能够再次搜索,我必须将它们的输入与“n”和“N”进行比较。我似乎无法让它正常工作。我不确定使用atow是否是正确的输入,但这里是代码。汇编语言再次搜索

number1   WORD  ? 
anArray   WORD  100 DUP (?) 
count   WORD  ? 
search   WORD  ? 
searchn   BYTE  "n",0 
searchNo   BYTE  "N",0 
prompt1   BYTE  "Enter a number or -1 to quit.", 0 
prompt2   BYTE  "Enter a number to search for", 0 
prompt3   BYTE  "Search for another number Y/N",0 
inString  BYTE  40 DUP (?) 
outMsgLabel  BYTE  "Search Result", 0 
frontOut1  BYTE  6 DUP (?) 
outMsg1   BYTE  " is element" 
rearOut1  BYTE  6 DUP (?),0 
frontOut2  BYTE  6 DUP (?) 
outMsg2   BYTE  " is not in array",0 

searchAgain: input  prompt3, inString, 40 
       atow  inString 
       mov   dx,ax 
       atow  searchn 
       mov   ax,"n" 
       cmp   ax,dx 
       je   end1 
       atow  searchNo 
       mov   ax,"N" 
       cmp   ax,dx 
       je   end1 
       jmp   next 

我只包括代码片段不工作和我的.DATA部分。

+0

写在一个高层次的语言,然后再转换。然后使用调试器进行调试。 – 2012-03-03 21:26:11

+0

我一直在使用调试器,但我正在学习大学课程中的汇编语言。我试着用C++编写它并转换它,但我找不到它。它似乎是将inString转换为一个单词,因为它在ax寄存器中产生0。我的老师从来没有教过我们如何比较字符串甚至转换字符串。 – todaroa 2012-03-03 21:30:42

+1

这是什么架构?它看起来有点像i86在masm中,但我不认识'atow'或者'input' ... – 2012-03-04 00:01:23

回答

0

为什么大家都说先把它写在HLL中?

你为什么使用WORD大小?使用32位处理器的自然大小。

这是什么汇编程序?如果它是MASM或其他任何输入,atow宏或函数?无论哪种方式好,你正试图inString的地址转换为文字而不是其内容

试试这个:

prompt3   BYTE "Search for another number Y/N ? >" 
PROMPT3_SIZE equ  $ - prompt3 



SearchAgain: 
    push PROMPT3_SIZE 
    push offset prompt3 
    call WriteToConsole 

    call GetKeyCode 
    cmp  eax, 78   ; N 
    je  end1 
    cmp  eax, 110  ; n 
    je  end1 
    cmp  eax, 65   ; Y 
    je  Continue   
    cmp  eax, 97   ; y 
    je  Continue 

    ; invalid char was entered, do whatever here 
    jmp  SearchAgain  

end1: 
    ; don't want to search again 
Continue: 
    ; search for character 

; ... 
; ... 
; ... 
    push 0 
    call ExitProcess 
WriteToConsole: 
    enter 0, 0 

    push STD_OUTPUT_HANDLE 
    call GetStdHandle 

    push 0 
    push 0 
    push [ebp + 12] 
    push [ebp + 8] 
    push eax 
    call WriteConsole 

    leave 
    ret  4 *2 

GetKeyCode: 
    push STD_INPUT_HANDLE 
    call GetStdHandle 

    push eax 
    call FlushConsoleInputBuffer 

    call crt__getch 
    xor  ecx, ecx  ; zero ecx for non extended key return value 
    test eax, eax 
    jz  @F 
    cmp  eax, 0E0h 
    jnz  quit 
@@: 
    call crt__getch 
    mov  ecx, 1   ; return 1 in ECX if an extended key is pressed 
quit: 
    ret