Assembly NASM:div指令工作不正常
我正在尝试编写16位代码,以便在十六进制值传递到dx寄存器时打印它。为了简单起见,在这个阶段,我假设传递的值只包含数字0-9,目前我正在尝试使用div指令,这只是挂断处理器或其他东西。它只留下闪烁的光标。通过网络提出的主要思想是将数字除以16,并在余数上加上一些值,以便进一步打印 我的print_hex.asm函数代码当前如下所示:Assembly NASM:div指令工作不正常,assembly,nasm,x86-16,Assembly,Nasm,X86 16,我正在尝试编写16位代码,以便在十六进制值传递到dx寄存器时打印它。为了简单起见,在这个阶段,我假设传递的值只包含数字0-9,目前我正在尝试使用div指令,这只是挂断处理器或其他东西。它只留下闪烁的光标。通过网络提出的主要思想是将数字除以16,并在余数上加上一些值,以便进一步打印 我的print_hex.asm函数代码当前如下所示: ; Print string function, note that this code written after jmp $ instruction, ; ot
; Print string function, note that this code written after jmp $ instruction,
; otherwise the string were printed twice
print_hex:
push ax ; pushing registers that used onto a stack
push dx
print_hex_loop:
mov dx, 16
mov ax, 16
div ax ; i think after this instruction execution stops somehow
mov ah, 0x0e ; scrolling teletype BIOS routine
add dx, 48
mov al, dl
int 0x10 ; print what in al
;cmp dx, 0
;jnz print_hex_loop
mov al, 0x0a ; line feed and carriage return added after printed number
int 0x10
mov al, 0x0d
int 0x10
pop dx ; popping registers that used from stack
pop ax
ret
此代码在此文件中调用:
[org 0x7c00]
mov bp , 0x8000
mov sp , bp
mov dx, 0x7c00
call print_hex
jmp $
%include "print_hex.asm"
times 510-($-$$) db 0
dw 0xaa55
如何正确使用div?在这里,我期待着,dx中的值16将被ax中的值16除以,剩余部分将在指令div之后的ax中(根据),但出现了一些错误:)您可能被符号
dx:ax
搞糊涂了,它不是除法。它意味着“由dx和ax形成的32位数字,dx为顶部16位,ax为底部16位”。但是要除以2的幂(16是1),你需要使用移位和掩码。另请参见好的,听起来我误解了div指令