十六进制数的二补码 在汇编语言中。

问题描述 投票:0回答:1

我试图弄清楚如何将十六进制数转换为二的补码十六进制数。我已经做了所有必要的检查,字符来作为十六进制数字,并将其转换为ASCII代码的数字代码,但当我试图打印转换后的Tow's补数,它给我一个意想不到的结果,我觉得我在这个代码中缺少的东西。谁能帮帮我,先谢谢你?

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
assembly x86 twos-complement irvine32
1个回答
0
投票

这个代码正确地将 "A "转换为0xF6,也就是-10,0x0A的两个补码。

但随后它试图将0xF6打印成一个字符串。为了打印它,需要将0xF6的值转换为可打印的字符。它还需要以空结束。

其他一些注意事项。输入是一个空结束的字符串,但输出缓冲区只有一个字节。如果输入超过一个字节,就会溢出输出缓冲区。你不能通过对每个字节分别进行二乘法来计算一个多字节数的二乘法。

© www.soinside.com 2019 - 2024. All rights reserved.