简体   繁体   English

汇编语言:如何比较8086中的输入数字?

[英]Assembly Language: How to compare an input number in 8086?

The problem is to ask a user to input any number. 问题是要求用户输入任何数字。 And compare the entered number with a pre-defined constant number. 并将输入的数字与预定义的常数进行比较。 Then output a message that number the entered is either greater or less than or equal to the defined number. 然后输出一条消息,指出输入的数字大于或小于或等于定义的数字。 In this example my predefined number is 27. 在此示例中,我的预定义数字是27。

My code is: 我的代码是:

   .MODEL small
   .STACK 100h
   .DATA
promptmsg DB 'Please enter a number [1..100]',13,10,'$'
greatermsg db 'You have entered a greater number', 13, 10, '$'
lessmsg db 'You have entered a lesser number', 13 , 10, '$'
correctmsg db 'You have hit the right number', 13, 10, '$'
numbr dw 27
   .CODE
   .startup
   mov  ax,@data
   mov  ds,ax                   
   lea dx, promptmsg
   mov  ah,9    
   int  21h                     
   mov ah, 0ah
   int 21h
   mov ah, 9
   int 21h
   mov bx, numbr
   cmp ax, bx
   jb lesser
   ja greater
correct:
   mov dx, offset correctmsg
   mov ah, 09h
   int 21h
   jmp endexe
greater:
   mov dx, offset greatermsg
   mov ah, 09h
   int 21h
   jmp endexe
lesser:
   mov dx, offset lessmsg
   mov ah, 09h
   int 21h
endexe:
   mov  ah,4ch                 ;DOS terminate program function
   int  21h                    ;terminate the program
   END

If I input any number regardless it is greater or less than or equal to the predefined number, it always jumps to greater. 如果我输入任何数字,无论该数字是大于还是小于或等于预定义的数字,它总是会跳到更大的数字。 Is there any problem with my code, that failed to recognize my input number? 我的代码有任何问题,无法识别我的输入号码吗?

Update: Corrected the variable numbr instead of ans. 更新:更正了变量numbr而不是ans。

I see this line in your code: 我在您的代码中看到此行:

mov bx, ans

However, I don't see 'ans' defined anywhere. 但是,我看不到在任何地方定义“ ans”。 Did you mean 'numbr'? 你是说'numbr'吗?

I see a few other problems. 我看到其他一些问题。 Your code seems to think that the result of int 21h/ah=0ah operation will return a number via the ax register. 您的代码似乎认为int 21h / ah = 0ah操作的结果将通过ax寄存器返回一个数字。 It won't. 不会的 According to the first reference I found while googling, the operation returns a string in the buffer referenced by ds:dx. 根据在谷歌搜索时发现的第一个参考 ,该操作在ds:dx引用的缓冲区中返回一个字符串。 Which, BTW, your code does not explicitly establish-- when int 21h/ah=0ah, is called, ds:dx still points to promptmsg, so the operation will overwite the prompt. 顺便说一句,顺便说一句,您的代码没有明确建立-当调用int 21h / ah = 0ah时,ds:dx仍指向hintmsg,因此该操作将覆盖提示符。

So it looks like the code prints the prompt, asks for input, and then prints the input back to the user. 因此,看起来代码将打印提示,要求输入,然后将输入打印回用户。 By the time it gets to the comparison: 到比较时为止:

cmp ax, bx

You have already put 9 into the upper half of ax, so it makes sense that the 'greater' path is always taken. 您已经将9放到了斧头的上半部分,因此始终采用“更大”路径是有意义的。 But again, that doesn't matter, since the read instruction doesn't return input data via ax, but rather in the buffer pointed to by ds:dx. 但这又没有关系,因为读指令不会通过ax返回输入数据,而是通过ds:dx指向的缓冲区返回输入数据。

However, even if you did load the first byte referenced by ds:dx, your code still would not work as you expect. 但是,即使您确实加载了ds:dx引用的第一个字节,您的代码仍然无法按预期工作。 You will need to convert a sequence of ASCII characters into a number. 您将需要将ASCII字符序列转换为数字。 Ie, '9' -> 9, '100' -> 100. That will require a little more code. 即'9'-> 9,'100'->100。这将需要更多代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用汇编语言8086将字符串转换为数字? - How to convert a string to number in assembly language 8086? "如何在 x8086 汇编语言中输入十进制时以十六进制显示两个负数的总和?" - How to display the summation of two negative number in HEX while the input is decimal in x8086 assembly language? 如何在微处理器8086中以汇编语言输入16位数字? - How can I input 16 bit number in assembly language in microprocessor 8086? 用8086汇编语言编程来比较两个数字 - Program in 8086 assembly language to compare two numbers 如何使用汇编语言将输入输入到新行中(Intel 8086) - How to get input onto a new line with Assembly Language (Intel 8086) 如何在 Emu8086 汇编语言中显示用户的输入 - How to display the user's input in Emu8086 Assembly Language 我如何在程序集 emu8086 中输入 2 位数字 - how can i input 2 digit number in assembly emu8086 汇编语言 [EMU 8086] - Assembly Language [ EMU 8086] 如何在emu8086汇编语言中输入8位二进制并转换为ASCII - How to take input 8 bit binary and convert into ASCII in emu8086 Assembly Language 使用8086汇编语言编写的程序:从数字输入数组中查找比单个输入数字大的所有数字的程序 - Program in 8086 assembly language : program that finds all bigger numbers than a single input number from an input array of numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM