汇编:将十六进制字符串转换为单字节

时间:2016-09-21 00:13:39

标签: assembly hex

在汇编中,假设你有一个十六进制字符串'0x2f'。你如何将它转换为值'0x2f'的单个字节?

示例(假设SI等于2个字符的十六进制字符串的位置,并且conv_hex2byte执行转换,将结果返回到AL):

jmp    main
foo    db '2f'
bar    db 0
main:
    mov    si, foo
    call    conv_hex2byte
    mov    byte [bar], al
    ; bar should now have a value of '2f'
    ret
conv_hex2byte:
    ; What would go here?
    ret

编辑:我已经尝试了conv_hex2byte ......

conv_hex2byte:
push    ebx
push    ecx
push    edx

; Clear general registers
xor eax, eax
mov bx, .tmp0
mov ecx, eax
mov edx, eax

.char:
lodsb
mov dl, al
cmp dl, '0'
jl  .is_hex
cmp dl, '9'
jg  .is_hex
sub al, '0' ; Make '0' = 0h
.write_val:
mov bx, .tmp0
add bx, cx
mov [bx], dl
inc cl
cmp cl, 2
jl  .char
mov ah, byte [.tmp0]
shl ah, 4
mov al, byte [.tmp1]
or  al, ah
xor ah, ah
;call   os_dump_registers
pop edx
pop ecx
pop ebx
ret

.is_hex:
cmp dl, 'F'
jg  .lower
sub dl, 55 ; Make 'A' = 0ah
jmp .write_val

.lower:
sub dl, 87 ; Make 'a' = 0ah
jmp .write_val

.tmp0   db 0
.tmp1   db 0

由于某种原因,它产生了错误的数字......即

push hex
call conv_hex2byte
hex db '20'

导致AX等于0x32而不是0x20

0 个答案:

没有答案