我正在创建引导加载程序,该引导加载程序应在变量和输出结果之前加上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 ; 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
答案 0 :(得分:1)
TL; DR :看来您的主要问题是使用MOV
指令将数字存储到内存中不会将值转换为字符串。您必须编写代码将整数转换为字符串。
您可以使用重复除法将寄存器(EAX)中的值转换为其他基数(十进制为10)。通用算法是
val = number to convert repeat digit = val MOD 10 ; digit = remainder of val/10 val = val DIV 10 ; val = quotient of val/10 digit = digit + '0' ; Convert digit to character value by adding '0' Store digit until val == 0 Print digits in reverse order
如果您的电话号码是1234:
您会发现,当我们反复除以10时,得到的数字4,3,2,1与我们想要的1,2,3,4相反。您可以想出一种处理字符串反转的机制。一种快速而肮脏的方法是按相反的顺序将数字推入堆栈,然后可以按正确的顺序将每个数字从堆栈弹出。
由于您尝试显示32位无符号数字,因此需要在EAX中用val
除以。用EDX:EAX(其中EDX设置为0)中的值完成10位的64位除法。x86指令DIV
计算商(在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_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 的非标准函数itoa
类似,其中传递的缓冲区的地址与该函数返回的地址相同。
通过消除用于反转字符串的推动和弹出,可以大大简化代码。可以通过在输出缓冲区中将出现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
脚注
CPU 386
并使用了32位寄存器 EAX ,因此我创建了在需要时使用32位寄存器的代码,否则使用了16位寄存器。这减少了使代码膨胀的不必要的指令前缀。结果,此代码只能在具有386+处理器的系统上以实模式运行。您可以使用16位寄存器进行32位除法,但这更加复杂,超出了此答案的范围。