16進数で数値を出力するためのx86アセンブリプログラムを作成しました。プログラムはnasmを使ってアセンブルされ、イメージファイルはqemuによって実行されました。プログラムのふるまいが私を混乱させました。以下の作業プログラムが示唆しているように、私はその数字の文字を印刷するために0x30を数字に追加する必要はありません。x86アセンブリ文字列バッファ番号からASCIIへ
; Boot sector code offset: 0x7c00
[org 0x7c00]
mov dx, 0x1fb6 ; The hexadecimal to be printed
call print_hex ; call the function
jmp $ ; jump infinitely
%include "print_string.asm" ; Include the print_string function
print_hex:
pusha ; push all registers to stack
mov ax, 0x4 ; rotate through the number four times
print_hex_loop:
cmp ax, 0x0 ; compare the counter with 0
jle print_hex_end ; if it is zero then jump to the end
mov cx, dx ; move dx to cx
and cx, 0x000F ; take the lower four binary digits of cx
cmp cx, 0xa ;compare the digits with 0xa
jge print_hex_letter ; if it is larger than a, jump to printing character
add cx, 0x0 ; otherwise print the ascii of a number
jmp print_hex_modify_string ; jump to routine for modifing the template
print_hex_letter:
add cx, 0x7 ; print the ascii of a letter
print_hex_modify_string:
mov bx, HEX_OUT ; bring the address of HEX_OUT into dx
add bx, 0x1 ; skip the 0x
add bx, ax ; add the bias
add byte [bx], cl ; move the character into its position
shr dx, 4 ; shift right 4 bits
sub ax, 0x1 ; subtract 1 from the counter
jmp print_hex_loop ; jump back to the start of the function
print_hex_end:
mov bx, HEX_OUT ; move the address of HEX_OUT to bx
call print_string ; call the function print_string
popa ; pop all registers from stack
ret ; return to calling function
HEX_OUT:
db '0x0000',0 ; The template string for printing
times 510-($-$$) db 0 ; fill zeros
dw 0xaa55 ; MAGIC_FLAG for boot
boot_sect.asm
print_string:
pusha
mov ah, 0x0e
mov al, [bx]
print_string_loop:
cmp al, 0x0
je print_string_end
int 0x10
add bx, 0x1
mov al, [bx]
jmp print_string_loop
print_string_end:
popa
ret
print_string.asm
このプログラムの出力は、私が期待したものですが、私はのASCIIコードを取得するために数字上の0x30のを追加しようとしたとき、数字は、出力は不器用だった。そこにいくつかのトリックですか、私はここでいくつかのポイントを欠場ですか?
ありがとうございます!
'HEX_OUT:db '0x0000''は**文字列**です。 0 'は既にあります。 ASCII '0'に初期化された値に0x30を追加する必要はありません。 'print_hex'を複数回呼び出しても、このコードが書かれた方法では動作しないことに注意してください。' HEX_OUT'が文字列 '0x0000'に初期化されることが予想されます。 –
ありがとう、ありがとう! –
'add byte [bx]、cl' ..' movバイト{bx}、cl'を実行する場合、最初に '' 0'''または '' A'-10'を追加する必要があります。 – Ped7g