如何在循环中使用WriteHexByte显示到屏幕上? [英] how do I display to the screen using writeHexByte in a loop?
本文介绍了如何在循环中使用WriteHexByte显示到屏幕上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
*WriteHexByte 需要8位寄存器 通过调用 ConvertHexNibble和使用 WriteChar
*WriteHexWord 需要16位寄存器或内存 参数,并显示每个字节 通过调用WriteHexByte
因此,在编写字符之后,它需要显示宏WriteChar实现的机器代码。并在循环中使用WriteHexBytes进行显示
尝试的代码
# Macro definition
.macro convertHexNibble register
cmpb $10,
egister
jl 1f
# process here is >=10
add $0x37,
egister
jmp 2f
# process here is <=10
1:
add $0x30,
egister
2:
# expect the register to now be the ascii code
.endm
#.macro writeHexByte register
#convert
#.endm
.macro displayChar character color
mov $0x09, %ah
mov character, %al
mov $0x00, %bh
mov color, %bl
mov $0x01, %cx
int $0x10
.endm
#set defualts values to move cursor off screen
.macro moveCursor row = $0xff column = $0xff
mov $0x02, %ah # ah = 02h moves the cursor row 2 col 2
mov $0x00, %bh # bh = 0 page
mov $0x02, %dh
mov $0x02, %dl # dl=1
int $0x10
.endm
.macro sumArray array number result
# assume array is an array of bytes
#therefore the accumulator is just %al (8bits)
mov $
umber, %cx # AX will serve as a counter for
# the number of words left to be summed
mov $0, %ax # BX will store the sum
mov array, %bx # CX will point to the current
# element to be summed
top: add (%bx), %cx
inc %bx # move pointer to next element
dec %ax # decrement counter
jnz top # if counter not 0, then loop again
done: mov %ax,
esult # done, store result in "array"
.endm
# The main boot processing code
.code16
.text
.global _start
_start:
mov $06, %al
convertHexNibble %al
displayChar %al, $0xe3
moveCursor %ah
mov $0x0A, %al
convertHexNibble %al
displayChar %al, $0xe3
sumArray $x 4 sum
# writtenHexWord $sum
# move Cursor off screen invisble
moveCursor
cli # clear interrupts
hlt
x: .byte 1, 5, 2, 10
# char x[] = {1, 5, 2, 10};
sum: .byte 0
# char sum = 0;
. = _start + 510
.byte 0x55
.byte 0xAA
推荐答案
WriteHexByte预期8位寄存器通过调用ConvertHexNibble并使用WriteChar来显示它
WriteHexWord需要16位寄存器或内存参数,并通过调用WriteHexByte显示每个字节
由于过度使用宏的,代码变得臃肿。每次调用宏时,在参数展开后,它的整个代码都会插入到调用发生的位置。使用宏的大部分操作应该使用子例程来完成。更短更快的程序!
尽管如此,我还是会向您展示一个基于您当前宏的解决方案。
- 必须通过对BIOS.Teleype函数0x0E的API调用来扩展displayChar宏,以便不会在相同的光标位置打印连续字符。
- 已通过先按0x30进行加法来优化ConvertHexNibble宏。这两种情况都必须发生,不是吗?
.macro displayChar character color
mov $0x01, %cx
mov $0x00, %bh
mov color, %bl
mov $0x09, %ah # BIOS.WriteColoredCharacter
mov character, %al
int $0x10
mov $0x0E, %ah # BIOS.Teletype
int $0x10
.endm
.macro convertHexNibble register
add $0x30,
egister
cmpb $0x39,
egister
jbe 1f
# process here is >=10
add $0x07,
egister
1:
# expect the register to now be the ascii code
.endm
.macro writeHexByte register
mov
egister, %al
push %ax
shr $4, %al
convertHexNibble %al
displayChar %al, $0xE3
pop %ax
and $15, %al
convertHexNibble %al
displayChar %al, $0xE3
.endm
.macro writeHexWord register
mov
egister, %ax
push %ax
writeHexByte %ah
pop %ax
writeHexByte %al
.endm
这是您的sum Array宏的更正版本,它真的、真的不应该作为宏存在!!
如果您注释";#假定数组是字节数组";,则何时将字添加到%cx
中的当前总和?
而大多数其他评论甚至与代码不匹配。
.macro sumArray array number result
# assume array is an array of bytes
# therefore the accumulator is just %al (8bits)
mov $
umber, %cx
mov array, %bx
xor %ax, %ax
top: add (%bx), %al
inc %bx # move pointer to next element
dec %cx # decrement counter
jnz top # if counter not 0, then loop again
mov %al,
esult # (*)
.endm
(*)因为sum被定义为一个字节(sum: .byte 0
),所以您的最终存储永远不应写入%ax
的完整单词。
这篇关于如何在循环中使用WriteHexByte显示到屏幕上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文