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

时间:2014-09-15 00:08:05

标签: assembly tasm

我需要在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

我需要一些正确实现的指导(我真的不是在汇编语法中,而且语言的逻辑对我来说非常不典型)

3 个答案:

答案 0 :(得分:1)

将字节拆分为2个半字节,然后使用16个条目的地图查找字符。

答案 1 :(得分:1)

你有两个主要任务:

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

2)转换十六进制字符串中的整数并输出它。

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

1)从表中读取十六进制字符(以下代码中的示例1)。每个半字节需要一个包含16个条目的表。半字节是一个4位的块。是的:您还可以创建一个包含256个条目的表来处理没有“nibblifying”的字节或一个包含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

答案 2 :(得分:0)

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

and al, 0Fh