使用nasm从dx打印十六进制 [英] Printing hex from dx with nasm

查看:228
本文介绍了使用nasm从dx打印十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上想用nasm打印dx寄存器的内容.因此,内容为16位十六进制数字,例如0x12AB.

I actually want to print the content of the dx register with nasm. Thereby the content is a 16 bit hex digit such as 0x12AB.

因此,我首先实现了一个能够打印字符串的功能:

Therefore I've first implemented a function which is able to print a string:

print_string:
    pusha
    mov ah, 0xe

print_character:
    mov al, [bx]
    inc bx
    or al, al
    jz print_done
    int 0x10
    jmp print_character

print_done:
    popa
    ret

您可以通过以下方式使用此功能:

You can use this function in this way:

mov bx, MSG
call print_string

MSG:
    db 'Test',0

现在我想拥有一个将十六进制转换为字符串的函数,以便print_string能够打印它.我在想类似的东西:

Now i want to have a function, which converts the hex to a string, so that print_string is capable to print it. I was thinking about something like that:

print_hex:
    pusha
    mov bx, HEX_OUT
    ; HEX_OUT is a kind of template string
    ; now i want to get the hex of dx into this template in order to be able to print it
    ; However I'm not sure how to manage this
    call print_string
    popa
    ret

HEX_OUT:
    db '0x0000', 0

不幸的是,我不确定如何从dx到bx(分别为HEX_OUT)获取十六进制.有人可以帮我吗,或者有人有主意吗?

Unfortunately I'm not sure how I get the hex from dx into bx, respectively the HEX_OUT. Can someone help me or does someone have an idea?

我想像这样在最后使用它:

I want to use it at the end like this:

mov dx, 0x12AA
call print_hex

已经谢谢您了!

更新:

如前所述,我可以像这样实现分离和打印:

As mentioned I could achieve the separating and printing like this:

print_hex:
    pusha
    mov bx, PREFIX
    call print_string

next_character:
    mov bx, dx
    and bx, 0xf000
    shr bx, 4
    add bh, 0x30
    cmp bh, 0x39
    jg add_7

print_character_hex:
   mov al, bh
   mov ah, 0x0e
   int 0x10
   shl dx, 4
   or dx, dx
   jnz next_character
   popa
   ret

add_7
   add bh, 0x7
   jmp print_character_hex

PREFIX:
   db '0x', 0

我尝试了类似的方法来使用我的函数和缓冲区打印它:

I tried something like this to print it with my function and the buffer:

print_hex:
    ;Added this here    
    mov cx, HEX_OUT + 2

print_character_hex:
    mov [cx], bh

尽管由于无效的有效地址",我无法进行汇编.我需要做些什么才能做到这一点?

Though I can't assemble this due to "invalid effective address". What do I need to do in order to accomplish this?

推荐答案

好的,我能够对其进行管理.谢谢您的帮助!这是工作代码:

All right, I was able to manage it. Thank you for your help! Here is the working code:

print_hex:
   pusha
   mov si, HEX_OUT + 2

next_character:
  mov bx, dx
  and bx, 0xf000
  shr bx, 4
  add bh, 0x30
  cmp bh, 0x39
  jg add_7

add_character_hex:
  mov al, bh
  mov [si], bh
  inc si
  shl dx, 4
  or dx, dx
  jnz next_character
  mov bx, HEX_OUT
  call print_string
  popa
  ret

add_7:
  add bh, 0x7
  jmp add_character_hex

HEX_OUT:
  db '0x0000', 0

这篇关于使用nasm从dx打印十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆