【问题标题】:Assembly : Convert 8 bit binary number to hexadecimal number汇编:将 8 位二进制数转换为十六进制数
【发布时间】:2014-09-15 00:08:04
【问题描述】:

我需要在 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 位输入) 我在谷歌上找到了一些指导,其中指出:

假设您已经在寄存器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

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

【问题讨论】:

  • 您是否尝试过阅读或遵循任何有关 ASM 的教程? Q 是关于 ASM 还是关于将二进制转换为十六进制?如果是前者,那么您可以找到类似的答案。您仍然应该尝试向我们展示您的尝试。 stackoverflow.com/help/how-to-answer
  • 是的,我正在开发一些关于代码可能是什么样子的基本概念(正如我之前所说,我还没有真正进入汇编)
  • @masss 程序应该如何接收输入?即,程序从哪里得到要转换的 8 位值?

标签: assembly tasm


【解决方案1】:

你有两个主要任务:

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

【讨论】:

  • 非常有帮助!谢谢你:)
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    and al, 0Fh
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-01
      • 2014-05-15
      • 2015-06-02
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 2017-04-12
      • 2018-06-24
      相关资源
      最近更新 更多