汇编实验四 [bx]和 loop 的使用

P1

写代码,写代码,写代码。

 

没有清除屏幕,所以可能不怎么好看。

将源代码程序中字数据 0403H→修改为 0441H,再次运行.

没什么好说的,换个字符。

P2

循环实现

写代码,写代码,写代码。

debug验证

栈实现

写代码,写代码,写代码。

这里有几个注意(不过我不确定我写的是最好的那一种)。

  1. SP 指向的位置(栈底)
  2. BX 填充的内容(littleendian
  3. BX 的递减方法(word

直接用G 得了

验证通过~

P3

首先完成代码。

因为要完成“将mov ax, 4c00h 之前的指令复制到0:200 处”,所以作为复制的 source  ds:[bx]  中 (dest: es:[bx] ),ds肯定要赋值为cs.

至于第9行的17H是怎么来的,这个可以算也可以反编译试一试。我这里是直接打好了反编译(下二图),得到代码长度为17H(0H-16H)。

验证通过。

SUMMARY

P1比较简单

P2 栈实现当时做的比较乱,调试了很多次才最终成功。还是对栈的存储有点遗忘。

总结一下屏幕输出。

一、首先,为什么是写入内存地址0xb8000 ?

不为什么

0xA0000 is the pointer address to the Graphical Mode and 0xB8000 is the pointer address to the Color Text Mode(mode 13h) and 0xB0000 is the pointer to the Monochrome text Mode.

二、为什么是写入0x0403 ?

When writing to video memory (starting @ 0xb8000) there are 2 bytes for every cell on the screen. The character to display is in the first byte, and the attribute in the second.

Each screen character is actually represented by two bytes aligned as a 16-bit word accessible by the CPU in a single operation. The lower, or character, byte is the actual code point for the current character set, and the higher, or attribute, byte is a bit field used to select various video attributes such as color, blinking, character set, and so forth. This byte-pair scheme is among the features that the VGA inherited from the EGA, CGA, and ultimately from the MDA.

BIOS Color Attribute is an 8 bit value where the low 4 bits represent the character color and the high 4 bits represent the background color. For example, to print a white character ‘A’ with black background, the “BIOS Color Attribute” would be set to the hexadecimal value 0x0F. The highest bit of the color attribute, which is also the highest bit of the background color can take over two functions. It can either have no influence on the background color making text blink when set, effectively limiting the available background colors to only eight, or if intensive background colors are enabled the full 16 colors become available but blinking is no longer available.

简单来说,就是每个在屏幕上打印的字符都由两个字节表示。低位字节为字符的ASCII码,高位字节为字符属性(主要是颜色)。其中高位字节的最高位为背景属性一部分或闪烁属性。接下来3位为背景色,然后是4位字体颜色,即

其中颜色代码为

实践是检验真理的唯一标准。

当写入0x7041时,由于Intel为little-endian, 所以实际上内存从低位到高位为

b800:07b8 41  -> 'A'
b800:07b9 70  -> 0111 0000

翻译一下,就是背景为浅灰色,字体为黑色的字符’A’

当写入0xb041时,内存从低位到高位为

b800:07b8 41  -> 'A'
b800:07b9 B0  -> 1011 0000

翻译一下,就是背景为浅蓝绿色,字体为黑色的闪烁的字符’A’ 

gif比较短而且没有仔细处理,所以可能闪烁看起来怪怪的。

以上。

REFERENCE

  1. https://stackoverflow.com/questions/33681795/how-to-write-to-screen-with-video-memory-address-0xb8000-from-real-mode
  2. https://en.wikipedia.org/wiki/BIOS_color_attributes
  3. https://forum.osdev.org/viewtopic.php?f=1&t=16333
  4. https://en.wikipedia.org/wiki/VGA-compatible_text_mode

CC BY-NC-SA 4.0 本作品使用基于以下许可授权:Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注