简体   繁体   English

如何将2的数字相乘并形成4的数字然后显示呢?

[英]How can i multiply 2 number together and form 4 digit and then display it?

So the program start like this: 所以程序开始如下:

.data
num db 22
multiplier db 20
divide db 10
digit1 db ?
digit2 db ?
digit3 db ?
digit4 db ?
.code
MOV AL,num
MUL multiplier
;----22 x 20 = 440
MOV BH,AH
;----AH stores 04?? AL stores 40?
;----40
DIV divide
MOV digit1,AH
MOV digit2,AL
;----04
MOV AL,BH
DIV divide
MOV digit3,AH
MOV digit4,AL

;---display
MOV AH,02H
MOV DL,digit1
INT 21H
;---same for digit2,3,4

What the program print out is bunch of hex number. 程序输出的是一堆十六进制数字。 Can someone tell me how to store the 3 digit number or 4 digit number. 有人可以告诉我如何存储3位数字或4位数字。 Is it store in AX or still AL? 它存储在AX还是AL中?

Why do you think ah may be 04 and al 40? 为什么你觉得ah可能是04 al 40?

I ask for two reasons. 我问有两个原因。

1) if you would debug it, the debugger should be able to show you current value of ah , al . 1)如果您要调试它,则调试器应能够向您显示ahal当前值。 Although it probably shows hexa form of ax , and you don't understand hexa numbers, so that may be like one more thing for you to learn. 尽管它可能显示ax六进制形式,并且您不了解六进制数,所以对于您来说,这可能又是另一件事。

2) you are missing then the point of "bits". 2)您缺少“位”点。 al is 8 bit of information, that means it can contain 2 8 different pieces of information. al是8位信息,这意味着它可以包含2 8条不同的信息。 As each bit can be 0 or 1 (two options), and you have 8 bits, positioned - order matters. 由于每个位可以是01 (两个选项),并且您有8位,所以位置很重要。 If you would check combinatorics theory, that's "two raised to eighth power" of all possible combinations, and that's 256. Now those combinations are most often used as binary numbers, when treated as numbers from 0 to 255 (check the binary form to see how the bit patterns are assigned to particular numbers, it's logical, each bit representing one of the powers of two, just as in our decimal format each digit is representing particular power of ten). 如果您要检查组合理论,则所有可能组合的“二乘幂提高到八分之一”,即256。现在,这些组合最常用作二进制数,当被视为0到255之间的数字时(请检查二进制形式以查看如何将位模式分配给特定数字,这是合乎逻辑的,每个位代表2的幂之一,就像在我们的十进制格式中,每个数字代表10的特定幂一样。

All gibberish talk you may think, show me the bug. 您可能会想到的所有胡言乱语,请告诉我该错误。 With your logic the number 100 would be split between ah:al as 01:00. 根据您的逻辑,数字100将在ah:al之间分配为01:00。 So maximum number in al would be 99. That's wasting another 3/5 of possible combinations (up to 255). 因此, al最大数量将为99。这浪费了3/5的可能组合(最多255)。

The 440 is in binary (bits) encoded as: 0000 0001 1011 1000 , first 8 bits are in ah , that equals to 1 , other 8 bits are in al , those equals to (440 - ah*256) = (440 - 1*256) = 184. (the value 256 in ax is split as 1 : 0 in ah : al , 255 is split as 0 : 255 ). 440以二进制(位)编码为: 0000 0001 1011 1000 ,前8位在ah ,等于1 ,其他8位在al ,那些等于(440-ah * 256)=(440-1 * 256)= 184(数值256ax被分割为10ahal255被分割为0255 )。 If you put values h : l into ah : al , the ax has value 256*h + l . 如果将值hl放入ahal ,则ax值为256*h + l

In debugger you would probably read: ax 01B8 . 在调试器中,您可能会读到: ax 01B8 The hexadecimal format has this neat feature, that each digit is exactly 4 bits long. 十六进制格式具有此整洁的功能,每个数字正好是4位长。 0 = 0000 , 1 = 0001 , B (11 in decimal) = 1011 , 8 = 1000 . 0 = 00001 = 0001B (11十进制)= 10118 = 1000 So the first two digits are content of ah , other two digits are content of al . 因此,前两位数字是ah内容,其他两位数字是al内容。

Read some more about bits, bytes, and do some math exercises to fully grasp how the conversion between binary <-> decimal <-> octal <-> hexadecimal works, just calculate it by hand few times. 阅读有关位,字节的更多信息,并进行一些数学练习,以完全掌握二进制<->十进制<->八进制<->十六进制之间的转换是如何工作的,只需手动计算几次即可。 Especially doing some basic dec <-> bin/hex math from head is very handy when programming in ASM, for example when you treat content of register as bit mask and not number, it's usually easier to write down the hex form of the value, than to recalculating all set bits into decimals and summing them. 特别是在ASM中进行编程时,特别是从头开始进行一些基本的dec <-> bin / hex数学运算非常方便,例如,当您将寄存器的内容视为位掩码而不是数字时,通常更容易记下该值的十六进制形式,而不是将所有设置的位重新计算为小数并求和。


Back to your code now (each paragraph is either a bug description, or at least very strong code style suggestion, you should follow, in case you want me ever to take a look on your source again). 现在回到您的代码(每段都是错误描述,或者至少是非常强烈的代码风格建议,如果您希望我再次查看您的源代码,则应该遵循)。

This is MASM or TASM I suppose. 我想这是MASM或TASM。 I don't like mov al,num , it hides the fact that you are actually getting the content of memory, not address. 我不喜欢mov al,num ,它掩盖了您实际上正在获取内存而不是地址的事实。 mov al,[num] is more exact definition of what's happening in CPU. mov al,[num]是CPU中发生的情况的更精确定义。 You can write it like that even in TASM (when needed, size of data is then specified by byte ptr [...] . 即使在TASM中,您也可以这样写(当需要时,数据大小由byte ptr [...]指定。

div byte ptr [mem8] divides ax with value in [mem8], in your case you divide 440 by 10. That will produce 0 remainder into ah , and quotient 44 into al . div byte ptr [mem8]划分ax在[mem8]值,你的情况,你把440 10.这将产生0余成ah ,和商44到al The "bug" is not in the instruction itself, but in your expectations. “错误”不是在指令本身中,而是在您的期望中。 You store ah as digit1 , but it's not font digit, it's value digit from 0 to 9. To turn it into font glyph, you need ASCII encoded digit. 您将ah存储为digit1 ,但它不是字体数字,而是从0到9的值数字。要将其转换为字体字形,需要ASCII编码的数字。 You have to transform it into ASCII encoding (check ASCII table to see which glyph has what code). 您必须将其转换为ASCII编码(检查ASCII表以查看哪个字形具有什么代码)。 So add ah,30h would fix digit1. 因此, add ah,30h将修复digit1。

Digit2 is completely off, as you can see, 44 is not even value in 0 to 9 range, so it can't be turned into single digit, you have to repeat the whole process, but this time starting with one order of 10 lower (so set ax to 44, not 440). 如您所见,Digit2完全关闭,0到9范围内的数值甚至都不是44,因此无法将其转换为一位数字,您必须重复整个过程,但是这次从低10的一个顺序开始(因此将ax设为44,而不是440)。 Then rewrite it into a loop. 然后将其重写为循环。

MOV AL,BH DIV divide - now you divide ax by byte 10 , while you didn't set AH . MOV AL,BH DIV divide -现在您将ax除以字节10 ,而未设置AH It can contain anything after that digit1 adventure, luckily for you in your version AH was 0 . 在digit1冒险之后,它可以包含任何内容,幸运的是,在您的AH版本中, AH0 Either do movzx ax,bh , or xor ah,ah to zero-extend the 8 bit value of bh into 16 bit ax . movzx ax,bhxor ah,ah可以将bh的8位值零扩展为16位ax (results of DIV and encoding into ASCII is explained above) (上面解释了DIV和编码为ASCII的结果)

Finally you display digit1 as first, but what was digit1? 最后,您首先显示digit1,但是digit1是什么? Remained of division by 10 of the original value. 保留除以原始值的10。 So for 440 you will accidentally output 0 , 4 , 4 , 0 (well, will not, as content of digit1-2-3-4 is not correct), looking like 440, but in reversed order. 因此,对于440,你一不小心就会输出0440 (当然,不会像digit1-2-3-4的内容是不正确的),恰似440,但以相反的顺序。 Try 22*49 = 1078 for debugging (if your code would produce correct digit# values, it would print 8701 then). 尝试22 * 49 = 1078进行调试(如果您的代码将产生正确的digit#值,则它将打印8701)。

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

相关问题 我如何在程序集 emu8086 中输入 2 位数字 - how can i input 2 digit number in assembly emu8086 如何更改此程序以获取 3 位数字并以二进制形式打印 - How can I change this program to take a 3 digit number and print it in binary 如何将对应的 (*) 值显示为 2 位十六进制数字在四位 7 段 LED 显示屏的最右边两位? - How do I display the corresponding (*) value as a 2-digit hexadecimal number on the rightmost two digits of the quad 7-segment LED display? 数学运算后如何用汇编语言显示2位数字? - How to display a 2 digit number in assembly language after mathematical operations? 如何将4位十六进制数字转换为带负数的二进制补码 - How can I convert a 4 digit hex number into Two's complement with neg 你好,我有一个问题,我需要从用户那里得到输入,输入是一个数字和一个又一个数字,这个数字可以是一个双字 - hello , i got a question that i need to get input from user, the input is a number and digit after digit ,the number can be a doubleWord 我如何在lc3中显示两位数数字 - how do i display double digit numbers in lc3 如何输入多于 1 位的数字? - How do i input a more than 1 digit number? 如何找到指令计数最少的模式数字? - How do I find the mode digit with the least number of instruction counts? 我如何在引导区中显示数字 - how can i display a number in my boot sector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM