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 byte [bx], cl'을하면 먼저 '0'또는 'A'-10'을 추가해야합니다. – Ped7g