十六进制数的二进制补码

时间:2020-06-07 06:12:45

标签: assembly x86 twos-complement irvine32

我试图弄清楚如何将十六进制数转换为二进制补码十六进制。我已经对该字符作为十六进制数字进行了所有必要的检查,并将其转换为ASCII代码的数字代码,但是当我尝试打印转换后的Tow的补码时,它给我带来了意外的结果,我觉得我在此代码中缺少了一些内容。有人可以帮忙谢谢你吗?

INCLUDE Irvine32.inc
.data

   var1 db "A",0
   var2 db "error",0

   char1 db ?

.code
main PROC

    mov  edx, OFFSET var1    ; get starting address of string

L1:
    mov  al, BYTE PTR [edx]  ; get next character
    inc  edx                 ; increment pointer
    test al, al              ; test value in AL and set flags
    jz   Finished            ; AL == 0, so exit the loop

    ; Otherwise, AL != 0, so we fell through.
    ; Here, you can do something with the character in AL.
    ; ...

    cmp al, 30h               ; compare it with 30H - ASCII CODE OF 0
    jb  error                 ; if charater is below 30H - then error

    cmp al, 39H               ; compare it with 39H - ASCII code of 9
    ja  Big                   ; if charater is above 30H 
                              ; check for A-F
    jmp comp                  ; otherwise, go for Conversion of ASCII code
Big:
    cmp al, 41H               ; compare al with 41H - ASCII code of A
    jb  error                 ; if code is below 41h, then error

    cmp al, 46H               ; compare al with 46H - ASCII code of F
    ja Small                  ; if code is above 46h, check for A-F

    jmp comp                  ; otherwise, go for Conversion of ASCII code
                              ; to numric value 
Small:
    cmp al, 61H               ; compare al with 46H - ASCII code of a
    jb  error                 ; if code is below 61H, then error

    cmp al, 66H               ; compare al with 46H - ASCII code of f
    ja error                  ; if code is above 66h, then error

comp:
    sub al, 30H               ; subtract 30H - numeric code
    cmp al, 09H               ; compare if al is in range of 0-9
    jbe next                  ; then jump to next to store back to Hex Array

    sub al, 07H               ; otherwise, for A-F, 7H has to be subtracted
    cmp al, 0FH               ; compare the value is in range of A-F

    jbe  next                 ; then jump to next to store back to Hex Array
    sub al, 20H               ; otherwise for a-f, subtract additional 20H
next: 
    not al                    ; get 1's complement 
    add al, 00000001b         ; add 1 to get 2't complement

    mov char1, al
    jmp L1



error:
    mov edx, OFFSET var2      ; display error massage
    call writestring

Finished:
    mov edx, OFFSET char1
     call writestring


    exit
main ENDP

END main

1 个答案:

答案 0 :(得分:0)

此代码正确地将“ A”转换为0xF6,即-10,即0x0A的两个补码。

但是随后它尝试将0xF6打印为字符串。为了进行打印,需要将值0xF6转换为可打印字符。还需要将其终止为null。

其他一些注意事项:
输入是一个以空值结尾的字符串,但是输出缓冲区只有一个字节。如果输入超过一个字节,则会溢出输出缓冲区。
您无法通过分别对每个字节进行两个补码来计算多字节数字的两个补码。