I've been interested in assembly for a while, and I've been writing a variety of different demos as a result, so now I'm sharing what I've learned before, and the things I need to download are nasm and qemu-system-i386, so I'll see how much energy I can generate from boring assembly.
Let's review the general purpose registers first:
8-bit general purpose register:
-
AL
: A data register used to store the lower 8 bits of the operand. -
AH
: A data register used to store the high 8 bits of the operand (usually associated with theAL
Used in combination to form a 16-bitAX
(registers). -
BL
: Another register used to store data, often used to set colors or as other parameters in BIOS interrupt calls. -
BH
: withBL
Used in conjunction to form a 16-bitBX
Registers. -
CL
: Commonly used for counters, e.g. in loops or displacement operations. -
CH
: withCL
Used in conjunction to form a 16-bitCX
Registers. -
DL
: Used to store data and can also be used to specify I/O port mapping. -
DH
: withDL
Used in conjunction to form a 16-bitDX
Registers.
16-bit general purpose register:
-
AX
: Accumulator for most arithmetic and data transfer operations. It isAL
cap (a poem)AH
The combination of the -
BX
: Base address register, often used to store memory addresses or as a loop counter. -
CX
: Count register, commonly used for loop counting and string manipulation. -
DX
: Data registers, commonly used for I/O operations and certain arithmetic operations.
These registers will be covered in a later demo, so keep an eye out for them; besides registers, you also need to understand interrupt numbers and service numbers. Simply put, the interrupt number identifies the interrupt handler, and the service number is used to internally select a service within the interrupt handler, as shown in the code.
; MBR bootloader example, prints a colored character 'H'
; NASM syntax
; Compiled by nasm
org 0x7C00 ; MBR start position
; Initialize BIOS data area (optional)
; mov [0x0500], byte 0x00
; Set the print position to the first row and column
mov dh, 4 ; line number (line number in BIOS starts from 0)
mov dl, 9 ; column number
mov bh, 0 ; page number, usually set to 0
mov ah, 0x02 ; set the service number of the cursor position
int 0x10
; print character 'H', set color attribute
mov ah, 0x09 ; print the service number of the character string
mov al, 'H' ; character to be printed
mov bl, 0x13 ; color attributes: foreground white (15), background blue (1)
mov bp, color_string ; Memory offset address of the string (the bp register is normally used to store the string address)
mov cx, 1 ; string length
int 0x10
; Fill remaining space and add MBR signature
; Ensure MBR size is 512 bytes
times 510 - ($ - $$) db 0
dw 0xAA55 ; MBR Signature
; Define the memory location to store the character
color_string db 'H', 0
This issue firstwill focus on a series of mov instructions, other parts of the code can be skipped directly, int 0x10 is used to access and control the video display interrupt, so whether we want to set the cursor or print characters need to use it. You can find a series of parameters are placed in different registers, and then trigger the interrupt, the interrupt is worth noting that the AH register, as I said earlier, it is the AX register of the high 8 bits, you need to place the service number to this AH register, first look at int 0x10 under the service number (need to be able to check the manual)
-
0x02
- Setting the cursor position- Moves the cursor in text mode to the specified position.
DH
register contains the line number.DL
register contains the column number.BH
The register contains the page number.
- Moves the cursor in text mode to the specified position.
-
0x03
- Getting the cursor position- Retrieves the current cursor position. After the call to
DH
register contains the line number.DL
register contains the column number.BH
The register contains the page number.
- Retrieves the current cursor position. After the call to
3. 0x09
- write a string
-
- Prints a string at the current cursor position.
AL
register contains the characters to be printed.BL
registers contain color attributes.CX
The register contains the number of repetitions.
- Prints a string at the current cursor position.
4. 0x0E
- print character
-
- Prints a character at the current cursor position.
AL
register containing the character to be printed.BL
The register contains color attributes.
- Prints a character at the current cursor position.
Currently want to do is to print a color string to the top of the screen, so first set the cursor position, determine the position and then print the string, combined with the comments and these register specifications will be better understood. Finished and then compiled with assembler nasm.
nasm
Then put it on top of qemu and run it.
qemu-system-i386 hello
The result unsurprisingly looks like this.
You need to pay attention to one thing, for example, don't forget to push the service number of the one that sets the cursor to the AH register, or there will be strange errors, such as different strings printed each time. In short, you must remember to set the service number before triggering the interrupt; also don't forget to add a 0 at the end of the definition string, which is similar to the C language string.
Once you change the number in the CX register, you can repeat the print.
The next installment will be more in-depth, and this is the first time I've compiled more relevant content, thanks for your attention~!