在程序集8086中将字符串十进制覆盖为二进制和十六进制 [英] Coverting String Decimal to Binary and Hexa in Assembly 8086
问题描述
我正在尝试将使用此代码读取的字符串转换为二进制和六进制.
I'm trying to convert a string I read with this code to binary and hexa.
READ_STRING:
MOV DX, offset buffer
MOV AH, 0Ah
INT 21h
MOV SI, 1d
MOV AX, 0
XOR CX, CX
MOV CL, buffer[SI]
INC SI
LOOP_1:
MOV DX, 10
MUL DX
MOV DL, buffer[SI]
SUB DL, 30h
MOV DH, 0
ADD AX, DX
INC SI
LOOP LOOP_1
RET
到目前为止,我已经将此代码用于二进制输出,但是它始终显示"1001"(十进制为9):
So far I have this code for binary output but it always prints "1001" (9 in decimal):
NEXT:
XOR AX, AX
XOR BX, BX
XOR CX, CX
MOV CL, 2
MOV AL, byte ptr[nombre]
MOV DI, offset binaire
; DIV : divide AX by CL. Remainder in AH and result in AL
LOOP:
DIV CL ; remainder in AH, quotient in AL
ADD AH, '0' ; 0 -> '0' , 1 -> '1'
MOV [DI], AH ; Saves the remainder in the array
INC DI
MOV AH, 0 ; reset AH for next division
CMP AL, 0 ; if result is 0, end
JNE LOOP
;Prints the binary number
MOV DX, offset binaire
CALL WRITE_STRING
谢谢!如果您还需要其他任何内容,请问.
Thanks! If you need anything else just ask.
推荐答案
在担心是否可以将值显示为二进制或十六进制之前;请检查以确保您的代码正确地将用户的输入转换为整数(例如,使用调试器).
Before worrying about whether or not you can display the value as binary or hexadecimal; check to make sure your code correctly converts the user's input into an integer (e.g. with a debugger).
对于二进制文件,请考虑以下内容:
For binary, consider something like this:
mov bx,ax ;bx = the value to display as binary
mov cx,16 ;cx = number of bits to display
mov di,offset binaire ;es:di = address to store string
.nextBit:
xor ax,ax ;al = 0
add bx,bx ;bx = value * 2; carry flag = overflow
adc al,0 ;al = '0' or '1'
stosb ;Add new character to string
loop .nextBit
mov byte [di],0 ;Terminate the string (ASCIIZ?)
mov dx, offset binaire
call WRITE_STRING
对于十六进制,这是相同的基本概念,不同之处在于,您需要提取最高的4位:
For hexadecimal, it's the same basic idea, except you need to extract the highest 4 bits:
mov bx,ax ;bx = the value to display as binary
mov cx,4 ;cx = number of nibbles to display
mov di,offset binaire ;es:di = address to store string
.nextNibble:
mov ax,bx ;ax = value
shr ax,12 ;ax = highest 4 bits of value
shl bx,4 ;bx = value << 4
add al,'0'
cmp al,'9'
jbe .gotChar
add al,'A' - '9'
.gotChar:
stosb ;Add new character to string
loop .nextBit
mov byte [di],0 ;Terminate the string (ASCIIZ?)
mov dx, offset binaire
call WRITE_STRING
注1:我没有测试过上面的任何代码,并且我通常使用NASM(而不是MASM),因此它可能不会按原样"汇编.
Note 1: I haven't tested any of the code above, and I normally use NASM (not MASM), so it may not assemble "as is".
注2:上面的示例代码特意简单.为了提高性能,您可以使用查找表来做得更好.
Note 2: Example code above is intentionally simple. For performance you could do a lot better using lookup tables instead.
注3:这些不是复杂的算法,您不必首先弄乱高级语言(除非您可能不了解二进制/十六进制转换的原理/数学).同样,一种语言看起来很优雅的算法在另一种语言上可能很丑陋(例如,您无法在C语言中以简单/简洁的方式检测到溢出,因此上面用于二进制转换的方法既不明显也不优雅)在C中;而在C中优雅的另一种方法可能会在汇编中带来严重的问题.
Note 3: These aren't complex algorithms and you shouldn't need to mess about with high level languages first (unless you don't understand the theory/maths behind binary/hex conversion perhaps). Also, an algorithm that seems elegant in one language can be an ugly mess in another language (e.g. you can't detect overflow in an easy/clean way in C, so the method used for binary conversion above wouldn't be obvious or elegant in C; and a different method that is elegant in C might suck badly in assembly).
这篇关于在程序集8086中将字符串十进制覆盖为二进制和十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!