Assembly 如何在引导加载程序中添加数字并将其显示到控制台?
我正在创建引导加载程序,它应该加上512到变量,并打印结果,直到达到指定的数字。对我来说,它是4194304,但问题是我真的不知道如何加上这些数字,因为在最后我总是什么也得不到或字符串损坏。那么,我应该如何更正加号呢Assembly 如何在引导加载程序中添加数字并将其显示到控制台?,assembly,x86,nasm,x86-16,bootloader,Assembly,X86,Nasm,X86 16,Bootloader,我正在创建引导加载程序,它应该加上512到变量,并打印结果,直到达到指定的数字。对我来说,它是4194304,但问题是我真的不知道如何加上这些数字,因为在最后我总是什么也得不到或字符串损坏。那么,我应该如何更正加号呢 cpu 386 bits 16 org 0h start: cld xor ax,ax mov ss,ax mov sp,7c00h ; setup stack mov ax,8000h mov es,ax
cpu 386
bits 16
org 0h
start:
cld
xor ax,ax
mov ss,ax
mov sp,7c00h ; setup stack
mov ax,8000h
mov es,ax ; initialize es w/ 8000h
mov ds,ax ; initialize ds w/ 8000h
;===============================================================================================================
load_prog:
mov ax,0206h ;function/# of sec to read
mov cx,0001h ;0-5 sec # (counts from one), 6-7 hi cyl bits
mov dh,00h ;dh=head dl=drive (bit 7=hdd)
mov bx,0h ;data buffer, points to es:0
int 13h
cmp ah,0
jne load_prog ;this is allowable because it is relative
;============================================================================================================
next:
mov eax, [NUMBERS]
add eax, 512 ;I think this have to plus numbers, so result have to be 512 = 0 + 512
mov [NUMBERS], eax ;And this i think have to store result to NUMBERS
print_1:
mov si, msg0
push ax
cld
printchar_1:
mov al,[si]
cmp al,0
jz print_2
mov ah,0x0e
int 0x10
inc si
jmp printchar_1
print_2:
mov si, [NUMBERS]
push ax
cld
printchar_2:
mov al,[si]
cmp al,0
jz print_3
mov ah,0x0e
int 0x10
inc si
jmp printchar_2
print_3:
mov si, msg1
push ax
cld
printchar_3:
mov al,[si]
cmp al,0
jz next
mov ah,0x0e
int 0x10
inc si
jmp printchar_3
done:
hlt
jmp done
;=====================================================================================================================
MBR_Signature:
msg0 db 'Counted numbers ',0
msg1 db ' of 4194304',13,10,0
NUMBERS dd 0
times 510-($-$$) db 0
db 55h,0aah
times 4096-($-$$) db 0
TL;DR:您的主要问题似乎是,使用
MOV
指令将数字存储到内存不会将值转换为字符串。必须编写代码才能将整数转换为字符串
可以使用重复除法将寄存器(EAX)中的值转换为不同的基数(十进制数字以10为基数)。一般算法是 如果您的电话号码是1234:
- 1234/10=123余数4(数字)
- 123/10=12余数3(数字)
- 12/10=1余数2(数字)
- 1/10=0余数1(数字)
- 完成
val
进行除法。用EDX:EAX(其中EDX设置为0)中的值乘以10进行64位除法。x86指令计算商(在EAX中返回)和余数(在EDX中返回)
我建议将常用代码移动到函数中,以减少重复,简化开发,并使代码更易于维护
创建一个函数uint32_to_str
,该函数使用重复的10除法,在计算ASCII数字时将其存储在堆栈上。最后,ASCII数字从堆栈中弹出并存储到传递给函数的缓冲区中。这与itoa
功能类似,因为数字总是写在缓冲区的开头。完成后,缓冲区以NUL(0)终止。功能原型可能如下所示:
; uint32_to_str
;
; Parameters:
; EAX = 32-bit unsigned value to print
; ES:DI = buffer to store NUL terminated ASCII string
;
; Returns:
; None
;
; Clobbered:
; None
您的代码还打印字符串。使用原型创建一个print\u str
函数:
; print_str
;
; Parameters:
; DS:SI = NUL terminated ASCII string to print
;
; Returns:
; None
;
; Clobbered:
; None
这些只是示例原型。您可以选择在您选择的寄存器中传递值和地址。您还可以决定函数是否返回值以及哪些寄存器被阻塞。在这段代码中,我保留了所有使用的寄存器。你可以选择保留部分或全部,这取决于你自己
然后,您的引导加载程序可能看起来像:
cpu 386
bits 16
org 0h
start:
cld
xor ax,ax
mov ss,ax
mov sp,7c00h ; setup stack
mov ax,8000h
mov es,ax ; initialize es w/ 8000h
mov ds,ax ; initialize ds w/ 8000h
;=================================================================================
load_prog:
mov ax,0206h ; function/# of sec to read
mov cx,0001h ; 0-5 sec # (counts from one), 6-7 hi cyl bits
mov dh,00h ; dh=head dl=drive (bit 7=hdd)
mov bx,0h ; data buffer, points to es:0
int 13h
cmp ah,0
jne load_prog ; this is allowable because it is relative
;=================================================================================
mov eax, [NUMBERS]
next:
add eax, 512 ; Advance value by 512
mov si, msg0
call print_str
mov di, strbuf ; ES:DI points to string buffer to store to
call uint32_to_str ; Convert 32-bit unsigned value in EAX to ASCII string
mov si, di ; DS:SI points to string buffer to print
call print_str
mov si, msg1
call print_str
cmp eax, 1024*4096 ; End loop at 4194304 (1024*4096)
jl next ; Continue until we reach limit
mov [NUMBERS], eax ; Store final value in NUMBERS
done:
hlt
jmp done
; print_str
;
; Parameters:
; DS:SI = NUL terminated ASCII string to print
;
; Returns:
; None
;
; Clobbered:
; None
print_str:
push ax
push di
mov ah,0x0e
.getchar:
lodsb ; Same as mov al,[si] and inc si
test al, al ; Same as cmp al,0
jz .end
int 0x10
jmp .getchar
.end:
pop di
pop ax
ret
; uint32_to_str
;
; Parameters:
; EAX = 32-bit unsigned value to print
; ES:DI = buffer to store NUL terminated ASCII string
;
; Returns:
; None
;
; Clobbered:
; None
uint32_to_str:
push edx
push eax
push ecx
push bx
push di
xor bx, bx ; Digit count
mov ecx, 10 ; Divisor
.digloop:
xor edx, edx ; Division will use 64-bit dividend in EDX:EAX
div ecx ; Divide EDX:EAX by 10
; EAX=Quotient
; EDX=Remainder(the current digit)
add dl, '0' ; Convert digit to ASCII
push dx ; Push on stack so digits can be popped off in
; reverse order when finished
inc bx ; Digit count += 1
test eax, eax
jnz .digloop ; If dividend is zero then we are finished
; converting the number
; Get digits from stack in reverse order we pushed them
.popdigloop:
pop ax
stosb ; Same as mov [ES:DI], al and inc di
dec bx
jne .popdigloop ; Loop until all digits have been popped
mov al, 0
stosb ; NUL terminate string
; Same as mov [ES:DI], al and inc di
pop di
pop bx
pop ecx
pop eax
pop edx
ret
;================================================================================
NUMBERS dd 0
msg0 db 'Counted numbers ',0
msg1 db ' of 4194304',13,10,0
; String buffer to hold ASCII string of 32-bit unsigned number
strbuf times 11 db 0
times 510-($-$$) db 0
MBR_Signature:
db 55h,0aah
times 4096-($-$$) db 0
函数的替代版本 我通常会使用跳转到循环中间的代码,以允许退出条件(字符为零)在循环的末尾而不是中间完成。这样可以避免在最后执行无条件JMP指令:
; print_str
;
; Parameters:
; DS:SI = NUL terminated ASCII string to print
;
; Returns:
; None
;
; Clobbered:
; None
print_str:
push ax
push di
mov ah,0x0e
jmp .getchar ; Start by getting next character
.printchar:
int 0x10
.getchar:
lodsb ; Same as mov al,[si] and inc si
test al, al ; Is it NUL terminator?
jnz .printchar ; If not print character and repeat
pop di
pop ax
ret
原始的uint32_to_str
设计为始终返回从传递的缓冲区开始的字符串。这类似于C的非标准函数,其中传递的缓冲区地址与函数返回的地址相同
通过删除用于反转字符串的推送和弹出按钮,可以极大地简化代码。这可以通过从输出缓冲区中出现NUL终止符的位置开始写入ASCII数字来实现。ASCII数字在计算时从字符串的末尾向开头插入缓冲区。从函数返回的地址可能在通过的缓冲区的中间。在以下代码中,数字字符串的开头通过DI寄存器返回给调用者:
; uint32_to_str
;
; Parameters:
; EAX = 32-bit unsigned value to print.
; ES:DI = buffer to store NUL terminated ASCII string.
; buffer must be at a minimum 11 bytes in length to
; hold the largest unsigned decimal number that
; can be represented in 32-bits including a
; NUL terminator.
; Returns:
; ES:DI Points to beginning of buffer where the string starts.
; This may not be the same address that was passed as a
; parameter in DI initially. DI may point to a position in
; in the middle of the buffer.
;
; Clobbered:
; None
uint32_to_str:
MAX_OUT_DIGITS equ 10 ; Largest unsigned int represented in 32-bits is 10 bytes
push edx
push eax
push ecx
mov ecx, 10 ; Divisor
add di, MAX_OUT_DIGITS ; Start at a point in the buffer we
; can move backwards from that can handle
; a 10 digit number and NUL terminator
mov byte [es:di], 0 ; NUL terminate string
.digloop:
xor edx, edx ; Division will use 64-bit dividend in EDX:EAX
div ecx ; Divide EDX:EAX by 10
; EAX=Quotient
; EDX=Remainder(the current digit)
add dl, '0' ; Convert digit to ASCII
dec di ; Move to previous position in buffer
mov [es:di], dl ; Store the digit in the buffer
test eax, eax
jnz .digloop ; If dividend is zero then we are finished
; converting the number
pop ecx
pop eax
pop edx
ret
脚注
- 我不确定您为什么在0x0000:0x8000将引导扇区和额外扇区读取到内存中,但我保留了该代码。这个代码可以工作,但我不确定你为什么要这么做
- 由于您使用指令
并使用32位寄存器EAX,因此我创建的代码在需要时使用32位寄存器,但在其他情况下使用16位寄存器。这样可以减少不必要的指令前缀,使代码膨胀。因此,此代码将仅在具有386+处理器的系统上以实模式运行。您可以使用16位寄存器进行32位除法,但它更复杂,超出了本答案的范围cpu386
cmp dword[NUMBERS],4194304
。当然,NUMBERS
应该是dd0
,因为您正在使用dword(4字节)操作数大小访问它。您使用的是旧版本的NASM,还是这不是错误消息的一部分?尝试组装它会产生预期的错误:未指定操作大小,而不是您声称得到的操作码和操作数的无效组合。我已经重新打开了它,因为OP修改了问题,所以它会进行组装,这实际上又回到了关于为什么代码看起来不工作的问题。虽然比较已经不存在了,但他的问题是为什么要打印垃圾或什么都不打印。是的,这是合理的,我们不需要再次重复模棱两可的操作数大小问题。现在他们删除了cmp
,所以它是一个无限循环?(并选择不使用添加dword[NUMBERS],512
)。它现在可能应该被关闭,因为它缺少对所发生事情的了解。加法很好,可能他们对BIOS中断所做的任何操作都没有打印出他们想要的内容,这并不奇怪在二进制整数上使用与在ASCII字符字符串上相同的循环。@MichaelPetch:对,如果SI
指向一个字符串(它们用于第一个和第三个,但不是第二个),则循环很好。但是我注意到它使用了m
; uint32_to_str
;
; Parameters:
; EAX = 32-bit unsigned value to print.
; ES:DI = buffer to store NUL terminated ASCII string.
; buffer must be at a minimum 11 bytes in length to
; hold the largest unsigned decimal number that
; can be represented in 32-bits including a
; NUL terminator.
; Returns:
; ES:DI Points to beginning of buffer where the string starts.
; This may not be the same address that was passed as a
; parameter in DI initially. DI may point to a position in
; in the middle of the buffer.
;
; Clobbered:
; None
uint32_to_str:
MAX_OUT_DIGITS equ 10 ; Largest unsigned int represented in 32-bits is 10 bytes
push edx
push eax
push ecx
mov ecx, 10 ; Divisor
add di, MAX_OUT_DIGITS ; Start at a point in the buffer we
; can move backwards from that can handle
; a 10 digit number and NUL terminator
mov byte [es:di], 0 ; NUL terminate string
.digloop:
xor edx, edx ; Division will use 64-bit dividend in EDX:EAX
div ecx ; Divide EDX:EAX by 10
; EAX=Quotient
; EDX=Remainder(the current digit)
add dl, '0' ; Convert digit to ASCII
dec di ; Move to previous position in buffer
mov [es:di], dl ; Store the digit in the buffer
test eax, eax
jnz .digloop ; If dividend is zero then we are finished
; converting the number
pop ecx
pop eax
pop edx
ret