繁体   English   中英

汇编:将8位二进制数转换为十六进制数

[英]Assembly : Convert 8 bit binary number to hexadecimal number

我需要在需要输入的tasm上编写汇编程序:8位二进制数,输出应为:相同数字的十六进制表示形式。 例如:10010110-> 96 10110100-> B4由于我在组装中是一个新手,所以我不知道从哪里开始解决这类问题。

编辑:这是我应该如何构造程序的基本思想:

.MODEL SMALL

.STACK 100H

.DATA
  PROMPT_1  DB  'Enter the binary number : $'
  PROMPT_2  DB  0DH,0AH,'The number of 1 bits is : $'
  HEX_MAp   DB  '1,2,3,4,5,6,7,8,9,A,B,C,D,E,F' 
  HEXA     DB      ?
.CODE
MAIN PROC
 MOV AX, @DATA                ; initialize DS  
 MOV DS, AX

 LEA DX, PROMPT_1             ; load and display PROMPT_1   
 MOV AH, 9
 INT 21H

 XOR BX, BX                   ; clear BX 
 MOV CX, 16                   ; initialize loop counter
 MOV AH, 1                    ; set input function

/好的,所以在加载包含二进制数的PROMT_1变量后,我们需要将其分成2个半字节(假设我们的输入始终是正确的8位输入),我发现与google相关的一些指导意见指出:

假设您已经有一个二进制数存储在寄存器ah ,例如10101100

mov al, ah 
shr al, 4 

因此,在al 00001010中,这是十六进制形式的第一个数字,如果小于10,则以十进制形式输出,否则输出字符(A,B,C,D,E,F)

接下来用其他4位数字做同样的操作

mov al, ah 
shl al, 4 
shr al, 4 

所以我们在al 00001100和输出结果

/

MOV AH, 4CH                  ; return control to DOS
INT 21H
MAIN ENDP
END MAIN

我在这里需要一些指导以实现正确的实现(我现在还不是汇编语法,这对我来说语言的逻辑也很不典型)

将字节分成2个半字节,然后使用16项映射查找字符。

您有两个主要任务:

1)从STDIN读取字符串并将其转换为计算机格式,将其转换为寄存器中的整数,并

2)将整数转换为十六进制字符串并输出。

您可以更轻松地分离任务并分别处理它们。 对于第二项任务,存在两种常用方法:

1)从表中读取十六进制字符(以下代码中的示例1)。 您需要一个带有16个条目的表,每个半字节。 半字节是4位的块。 是的:您还可以创建一个包含256个条目的表来处理一个字节而不会“混淆”,或者创建一个包含65536个单词的表(在16位模式下会有些困难)。

2)计算每个半字节的ASCII代码(以下代码中的示例2)。

我希望这段代码对您有所帮助:

.MODEL SMALL
.STACK 1000h

.DATA
  HEX_Map   DB  '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
  HEX_Out   DB  "00", 13, 10, '$'   ; string with line feed and '$'-terminator

.CODE

main PROC
    mov ax, @DATA                   ; Initialize DS
    mov ds, ax

    ; Example No. 1 with output
    mov di, OFFSET HEX_Out          ; First argument: pointer
    mov ax, 10101100b               ; Second argument: Integer
    call IntegerToHexFromMap        ; Call with arguments
    mov ah, 09h                     ; Int 21h / 09h: Write string to STDOUT
    mov dx, OFFSET HEX_Out          ; Pointer to '$'-terminated string
    int 21h                         ; Call MS-DOS

    ; Example No. 2 with output
    mov di, OFFSET HEX_Out          ; First argument: pointer
    mov ax, 10101100b               ; Second argument: Integer
    call IntegerToHexCalculated     ; Call with arguments
    mov ah, 09h                     ; Int 21h / 09h: Write string to STDOUT
    mov dx, OFFSET HEX_Out          ; Pointer to '$'-terminated string
    int 21h                         ; Call MS-DOS

    mov ax, 4C00h                   ; Int 21h / 4Ch: Terminate program (Exit code = 00h)
    int 21h                         ; Call MS-DOS
main ENDP

IntegerToHexFromMap PROC
    mov si, OFFSET Hex_Map          ; Pointer to hex-character table

    mov bx, ax                      ; BX = argument AX
    and bx, 00FFh                   ; Clear BH (just to be on the safe side)
    shr bx, 4                       ; Isolate high nibble (i.e. 4 bits)
    mov dl, [si+bx]                 ; Read hex-character from the table
    mov [di+0], dl                  ; Store character at the first place in the output string

    mov bx, ax                      ; BX = argument AX (just to be on the safe side)
    and bx, 00FFh                   ; Clear BH (just to be on the safe side)
    and bl, 0Fh                     ; Isolate low nibble (i.e. 4 bits)
    mov dl, [si+bx]                 ; Read hex-character from the table
    mov [di+1], dl                  ; Store character at the second place in the output string

    ret
IntegerToHexFromMap ENDP

IntegerToHexCalculated PROC
    mov si, OFFSET Hex_Map          ; Pointer to hex-character table

    mov bx, ax                      ; BX = argument AX
    shr bl, 4                       ; Isolate high nibble (i.e. 4 bits)
    cmp bl, 10                      ; Hex 'A'-'F'?
    jl .1                           ; No: skip next line
    add bl, 7                       ; Yes: adjust number for ASCII conversion
    .1:
    add bl, 30h                     ; Convert to ASCII character
    mov [di+0], bl                  ; Store character at the first place in the output string

    mov bx, ax                      ; BX = argument AX (just to be on the safe side)
    and bl, 0Fh                     ; Isolate low nibble (i.e. 4 bits)
    cmp bl, 10                      ; Hex 'A'-'F'?
    jl .2                           ; No: skip next line
    add bl, 7                       ; Yes: adjust number for ASCII conversion
    .2:
    add bl, 30h                     ; Convert to ASCII character
    mov [di+1], bl                  ; Store character at the second place in the output string

    ret
IntegerToHexCalculated ENDP

END main                            ; End of assembly with entry-procedure

为了分离AL寄存器的低字节,我们可以简单地使用:

and al, 0Fh

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM