assembly 汇编语言中的八进制到十六进制

vql8enpb  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(152)

我已经写了代码,但它并没有相应地转换。

.MODEL SMALL
.STACK 100H

.DATA
    INPUT_MSG DB 'Enter an octal number: $'
    OUTPUT_MSG DB 'The hexadecimal number is: $'
    INPUT_NUM DW ?
    HEX_NUM DB 3 DUP(0)
    HEX_DIGITS DB '0123456789ABCDEF'

.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX
    
    ; Display input message
    LEA DX, INPUT_MSG
    MOV AH, 9
    INT 21H
    
    ; Read octal input number
    MOV AH, 01H
    INT 21H
    SUB AL, 30H ; convert ASCII digit to binary
    MOV BL, AL ; save input number in BL
    
    MOV AH, 01H
    INT 21H
    SUB AL, 30H ; convert ASCII digit to binary
    MOV CL, AL ; save input number in CL
    
    MOV AH, 01H
    INT 21H
    SUB AL, 30H ; convert ASCII digit to binary
    MOV DL, AL ; save input number in DL
    
    ; Convert octal to hexadecimal
    MOV AL, BL ; load first digit
    AND AL, 07H ; mask upper bits
    MOV BH, 00H ; clear high byte of BX
    MOV BL, AL ; move first digit to low byte of BX
    MOV AL, BH ; clear AL
    MOV BL, HEX_DIGITS[BX] ; lookup hexadecimal digit
    MOV HEX_NUM[0], BL ; store first digit in HEX_NUM
    
    MOV AL, CL ; load second digit
    AND AL, 07H ; mask upper bits
    MOV BH, 00H ; clear high byte of BX
    MOV BL, AL ; move second digit to low byte of BX
    MOV AL, BH ; clear AL
    MOV BL, HEX_DIGITS[BX] ; lookup hexadecimal digit
    MOV HEX_NUM[1], BL ; store second digit in HEX_NUM
    
    MOV AL, DL ; load third digit
    AND AL, 07H ; mask upper bits
    MOV BH, 00H ; clear high byte of BX
    MOV BL, AL ; move third digit to low byte of BX
    MOV AL, BH ; clear AL
    MOV BL, HEX_DIGITS[BX] ; lookup hexadecimal digit
    MOV HEX_NUM[2], BL ; store third digit in HEX_NUM
    
    ; Display output message and hexadecimal number
    LEA DX, OUTPUT_MSG
    MOV AH, 9
    INT 21H
    
    MOV DL, HEX_NUM[0]
    MOV AH, 02H
    INT 21H
    
    MOV DL, HEX_NUM[1]
    MOV AH, 02H
    INT 21H
    
    MOV DL, HEX_NUM[2]
    MOV AH, 02H
    INT 21H
    
    ; Exit program
    MOV AH, 4CH
    INT 21H
MAIN ENDP

END MAIN
hyrbngr7

hyrbngr71#

INPUT_MSG DB 'Enter an octal number: $'

只有当你开始组合八进制数字(复数)的一部分时,输入才变成“八进制数字”(单数)。
八进制数字在数字的二进制表示中占据3位,因此3位八进制数的分解为:

FirstDigit * 64 + SecondDigit * 8 + ThirdDigit

在汇编中,乘以8转化为向左移位3倍,乘以64是向左移位两次的结果。
假设用户输入有效(仅使用数字“0”至“7”且不超过377o),代码变为:

; Read octal input number
MOV  CL, 3   ; CONST (to shift 3x to the left) [8086]
MOV  AH, 01h
INT  21h
SUB  AL, 30h ; convert ASCII digit to binary
MOV  BL, AL  ; Add FirstDigit

MOV  AH, 01h
INT  21h
SUB  AL, 30h ; convert ASCII digit to binary
SHL  BL, CL  ; Make room to add the following digit
             ; This is the 1st time that FirstDigit gets shifted to the left, so *8
OR   BL, AL  ; Add SecondDigit

MOV  AH, 01h
INT  21h
SUB  AL, 30h ; convert ASCII digit to binary
SHL  BL, CL  ; Make room to add the following digit
             ; This is the 2nd time that FirstDigit gets shifted to the left, so *64
             ; This is the only time that SecondDigit gets shifted to the left, so *8
OR   BL, AL  ; Add ThirdDigit

BL寄存器现在保存八进制数。作为示例,考虑八进制数253o,其在寄存器中留下十进制值171(2 * 64 + 5 * 8 + 3)。
下一个任务是将该值打印为十六进制数ABh。十六进制数字在寄存器中占用4位,我们需要首先打印最高有效位:

; Display output message and hexadecimal number
MOV  DX, OFFSET OUTPUT_MSG
MOV  AH, 09h
INT  21h

MOV  BH, 0
MOV  SI, BX
MOV  CL, 4              ; CONST (to shift 4x to the right) [8086]
SHR  BX, CL             ; Only keep most significant hex digit
MOV  DL, HEX_DIGITS[BX] ; lookup hexadecimal digit
MOV  AH, 02h
INT  21h
AND  SI, 15             ; Only keep least significant hex digit
MOV  DL, HEX_DIGITS[SI] ; lookup hexadecimal digit
MOV  AH, 02h
INT  21h

相关问题