首页 > 解决方案 > 如何使用标签和寄存器作为偏移量从内存复制到寄存器中?

问题描述

我正在尝试创建一个 print_hex 函数,它将寄存器 bx 中的十六进制值转换为字符数组,我正在努力理解为什么 nasm 告诉我我有一个无效的有效地址。我的 print_hex 函数如下所示:

print_hex:
pusha

; shift counter
mov ax, 12

ph_loop:
    ; if bx is 0 just print the predefined "0000" on hex_buff
    test bx, bx
    jz ph_loop_end

    mov cx, bx
    ; mask cx to have only the first character
    and cx, 0x000F
    ; use cx as the offset in the table and bring the character into cx (this is where the compile error happens)
    mov cx, [hex_table + cx]
    ; copy into a buffer the character that i just brought (using cl because for some reason if I use cl it copies 2 characters from the table)
    mov [hex_buff], cl

    shr bx, ax
    ; decrement counter by 4
    sub ax, 4
    jmp ph_loop
ph_loop_end:

mov bx, hex_buff
call print_string

popa
ret

更多信息,这是作为引导扇区程序运行的。我已经阅读了几篇关于使用 lea 的文章,但我不确定我是否可以在这里使用它。

标签: assemblynasmx86-16addressing-mode

解决方案


推荐阅读