Grey wolf Assembly video notes

Source: Internet
Author: User

Grey wolf Assembly video notes

----------------- 1, 2, 3 -----------------------------
Base Address for storing segment registers

AxGeneral registers

CS: The base address code segment for storing the code to be executed by the CPU
The IP alias is the instruction pointer register, which stores the offset address of the segment address.
CS * 16 + IPIs the command to be executed by the CPU.

Debug is a real-mode program debugging tool provided by DOS and Windows. It allows you to view the content in various CPU registers and run the machine code-level tracking program.

The R command is used to view and change the content of each register,
D command to view the content in the memory,
The U command converts the memory machine code into an assembly command,
Command a writes commands in memory in the format of Assembly commands
T command but not tracking

Data Segment: global variable
Code segment: Code
Stack segment: local variable

The CPU uses the DS (Data Segment) Register and the value of any common register or other numerical values to form the physical address of the data segment, for example:
DS: [0] DS: [BX](Memory addressing)(Memory Access)

MoV DS: [13abh], 1234 H       The content of the memory address is assigned a value.Memory Address = 1234
MoV [13abh], 1234 H       CPU points to DS by default

--------------------- 4 -----------------------------

How does the CPU know that a piece of memory space is used as a stack? How do I know which unit is the top unit of the stack?
The CPU uses the SS register and the SP General Register to perceive the existence of the stack segment.
The base address of the SS storage, and the offset address of the SP storage stack top. At any time, the SS: SP points to the top element of the stack.

--------------------- 5, 6 -----------------------------

How to define variables in assembly languages
How to display on screen
How to debug

Assume keywords
How can we let the Assembly Language "know" How many segments the application has written.
Assume indicates that a register is associated with a segment defined by segment... ends in the program.

DB commands
Define byte
Label dB initializer, initializer, initializer
Label indicates an optional label, which is equivalent to the C language variable name.
Msg db "Hello World"

VGA b800f
 Font attribute format
76 5 4 3 2 1 0
BL r g B I r g B

Flashing background color highlight foreground color
Red-Green: 0000010b

VGA video memory address space
Display 25 rows and 80 columns in 80*25 columns color mode
Each character can have 256 attributes (the background color, the foreground color, and so on)
A character occupies two bytes in the video memory, storing the ASCII code value and attributes respectively.
The display buffer is divided into eight pages, each page is 4 kb. The video card can display any page content. Generally, the 0th page content is b8000h ~ B8f9fh

Es, extended segment register

Do not assign values to segment registers directly. assign values to common registers before passing values to segment registers.

Loop keyword
       Command 2
      Look label
The cycle factor specified in CX as the loop

Memory Access
Helloworld is a data segment, so each character can be stored in the DS register through the data segment address. To obtain the first byte of the data segment, it must be represented as follows:
DS: [0] DS: [Si]
Si registers are equivalent to General registers.

The code segment address is automatically obtained and the offset address is unknown.

; Hello. ASM ;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Assume Cs: code, DS: Data

Data Segment
 DB "Hello World"
Data ends

Code segment
 MoV ax, Data
 MoV ds, ax
 MoV BX, 0b800h
 MoV es, BX
 MoV CX, 11
 MoV Si, 0
 MoV BX, 0
 MoV ah, 0000010b
 S: mov Al, DS: [Si]
   MoV ES: [BX], ax
   MoV ES: [bx + 1], ah
   INC Si
   Add Bx, 2
   Loop s
   MoV ax, 4c00h
   Int 21 h
Code ends
End start

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;

------------------- 7 -----------------------------

What is interruption?
Any general-purpose CPU has the ability to detect a special information generated from outside or inside the CPU after executing the currently executed command,
And immediately process the received information process. This information is called the interrupt information.
Interruption means that the CPU does not continue to execute (the command just executed) down, but instead forwards the special information.

Questions about interruptions
How does the CPU find the interrupt handler when the interrupt occurs?
There are many types of Interrupt handlers, so where is each interrupt handler?
After the interrupt processing program is completed, how does the CPU continue to run the previously interrupted program?

Interrupt vector table
Stores the data in the memory, and stores the interrupt handler entry corresponding to the 256 interrupt sources.
The interrupt vector table is generally stored in the memory at to 0000: 03fe.

A table item stores an interrupt vector, that is, an interrupt program entry address. This address includes a segment address and an offset address. Each table occupies two words (4 bytes ), the address of the High-address storage segment and the offset address of the Low-address storage segment.

In case of interruption, the CPU temporarily saves the Cs and IP memory in the stack. After the interrupt program is executed, the original CS and IP values are re-obtained through the out-stack command (this is the specific implementation of C function calls)
The CPU automatically completes these operations during the interruption process.

Div command
Division command: div register
Divisor: there are two types: 8-bit and 16-bit, in one register or memory unit.
Divisor: it is put in ax or DX by default. If the divisor is 8 bits, 16 bits are put in ax by default. If the divisor is 16 bits, 32 bits are placed in ax and DX respectively, DX stores high, and ax stores high positions.
Result: if the number is 8 bits, Al stores the Division operator, Ah stores the Division remainder. If the division is 16 bits, ax stores the remainder.

Modify the interrupt table:
How can we let the CPU not execute the original interrupt processing program, but execute our own processing program?
You can modify the entry address of the interrupt vector table.

------------------- 8 ----------------------
How to modify the interrupt vector table?
By default, the system stores the interrupt vector table from to 0000: 03fe. Each table occupies two words.
Then we know that the memory address of the interrupt table entry 0 is four bytes starting. The Assembly Code is to assign the four bytes to the entry address of the interrupt handler.
The Assembly pseudocode is as follows:
  MoV DS: [0]. We interrupt the handler's own offset address.
  MoV DS: [2]. The address of the interrupt handler segment.

The interrupt can be generated at any time. When the interrupt occurs, the interrupt processing program must be executed immediately. Where should the interrupt processing program be stored in the memory?
Ensure that the storage location of the interrupt handler cannot be overwritten by other programs at any time.
Therefore, we must find out in the memory that a piece of space is not applicable to any program.
Under normal circumstances, the memory address ranges from to, which is not used by other programs.

Interrupt Handler Memory Distribution
When an interrupt is triggered, the program will be executed, but the program begins with a data definition command instead of a code execution command. How can this problem be solved?
We want to jump to the assembly code that shows the string to execute the interrupt processing program at the beginning, then we need to use the Assembly command: Jump

The jump command jump is divided into three types:
Jump between segments to the jump far label                    Change the Cs and IP register values to the memory address of the label.
Segment jump command jump near label               Modify only the offset address with the IP register value as the label.
Segment short jump command jump short label            If the Cs and IP values are not modified, the compiler automatically calculates the jump location, which cannot exceed 256

------------------- 9 --------------------------
How Can I copy a piece of assembly code to a specified memory location?
It can be implemented using loop statements, but this statement is cumbersome and inappropriate.
The assembly language provides the rep and movsb commands to implement the same functions.
Movsb command:
Byte transfer command: The command transfers strings between storage units
When the movsb command is used, DS: Si points to the first address of the string to be copied, and ES: Di points to the destination address of the string to be copied.
The direction of the CLD command to copy data is from low bytes to high bytes, that is, each copy of a byte Si and di plus 1.
The STD command is opposite to ClD.

; Ins. ASM ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Assume Cs: Code

Code segment
 Step 1: modify the content of Table 0 in the interrupt vector table to point to the entry address of our own interrupt handler.
 MoV ax, 0
 MoV ds, ax
 MoV word ptr ds: [0], 0200 H
 MoV word ptr ds: [2], 0
 Step 3: copy the prepared interrupt handler 0 to the memory address pointed to by Table 0 in the interrupt vector table
 ; :0200
 MoV ax, CS
 MoV ds, ax
 MoV Si, offset int0; DS: Si available copy Source Address
 MoV ax, 0
 MoV es, ax
 MoV Di, 200 h; destination address set ES: Di
 MoV CX, offset int0end-offset int0; calculate the total memory occupied by the program
 Rep movsb; automatic use of ES: Di, DS: Si, CX
 Step 4: use the code to automatically trigger the No. 0 interrupt handler
 MoV ax, 1000 h
 MoV BH, 1
 Div BH
 MoV ax, 4c00h
 Int 21 h
 Step 2: write your own interrupt handler to display the string in the center of the screen
  Int0: JMP short int0start
    DB "I am student"
 Int0start: mov ax, 0b800h
 MoV es, ax; Configure the first video address
 To copy the strings one by one to the memory address space
 MoV ax, CS
 MoV ds, ax
 MoV Si, 202 h
 MoV Di, 12*160 + 36*2
 MoV CX, 12
 S: mov Al, DS: [Si]
 MoV ES: [di], Al
 Add Di, 2
 Loop s
 MoV ax, 4c00h
 Int 21 h
 Int0end: NOP
Code ends
End start

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;

------------------- 10 --------------------------
Main Content
Development Environment Construction
Understanding the boot program
NASM assembly
Write the first startup program

Visual pc2004 is used to start the startup program we wrote.
VMware runs Linux and uses the NASM compiler to compile the Startup Program on Linux.

Knowledge preparation
Understand the computer power-on Process
BiOS allocation of system memory
Understanding the concept of a Bootstrap program
Use NASM for compilation
Create a floppy Image

After we press the power button, how does the computer run the operating system from scratch?
After you press the power-on button, an electrical signal is sent to the BIOS.
After the BIOS obtains the electrical signal, it starts the self-check program and checks whether the peripheral device is powered on.
After the check, the self-check program returns the control to the bios, And the BIOS reads the boot program from the boot drive.

When the system powers up, the first 1 MB of memory is prepared for us by the bios, as shown below:
0x00000 ~ 0x003ff Interrupt vector table
0x00400 ~ 0x004ff BIOS data Zone
0x00500 ~ 0x07bff Free memory Zone
0x07c00 ~ 0x07dff Bootstrap loading zone 512 bytes
Ox07e00 ~ 0x9ffff Free memory Zone
0xa0000 ~ 0 xbffff Display memory Zone
0xc0000 ~ 0 xfffff Interrupt handling program

Understanding the boot program
What kind of program can be called a Bootstrap program?
The BIOS loads 512 bytes of the first sector of the disk to the memory and places them at 0x0000: 0x07c00.
If the last two bytes of the first sector are 55aa, It is a Bootstrap program.

Bootstrap features
The size is 512 bytes, which cannot be more than or less, because the BIOS only reads B to the memory.
It must end with 55aa, which is the guide sector identifier.
It is always placed on the first sector of the disk (0 head 0 this sector 1) because the BIOS only reads the first sector.

NASM assembly
It is a 80*86 assembler designed for portability and modularization. It supports a considerable number of target file formats, including Linux and Windows

Compile the Bootstrap program
How to Use NASM to compile a Bootstrap program?
What is NASM? What is the difference between NASM and MASM?
In Linux, how does one install NASM?
How to Use NASM to compile compiled assembly code?

Differences between NASM and MASM
NASM has a fairly simple memory reference rule. It is required to add square brackets to the address for any access to the content in the memory. However, any operation on the address value is not required.
For example, mov ax and bar commands assign the bar address to the ax register, which is equivalent to mov ax and offset bar in MASM.
The value of the bar variable is mov ax, [bar].
MASM mov ax, ES: Di    NASM MoV ax, [ES: di]

Install NASM in Linux, download the RPM package of NASM, and install rpm-ivh nasm ***. rpm
Use: NASM hello. ASM-O hello, disassembly: ndisasm hello

------------------- 11 -----------------------------

BiOS interrupt program
The system BIOS provides a large number of Interrupt handlers for us to call. Among them, the interrupt handler numbered 10 h is dedicated for display.
Note: A 10 h interrupt not only provides one program, but also provides many subprograms for us to call.

$ And $ keywords
In NASM, $ indicates the offset address of the current command.
In NASM, $ indicates the starting address of the instruction.
Therefore, we can calculate the formula for the number of remaining bytes.
Remaining bytes = 510-($-$)

; Boot. ASM ;;;;;;;;;;;;;;;;

Our startup program implements simple functions. Print a line of strings in the center of the screen.

Org 07c00h; the org command clearly tells the compiler that the segment address of my program is 7c00h, instead of the original 0000
The Int Assembly command "int 10 h" calls the interrupt program in Bois: displays the string

 MoV ax, CS
 MoV es, ax
 MoV bp, msgstrES: the content that BP points to is the string address we want to display.
 MoV CX, 12 ; The length of the displayed string
 MoV DH, 12 ; The displayed row number
 MoV DL, 36 ; Column number displayed
 MoV BH, 0  ; Number of pages displayed
 MoV Al, 1  ; Displays the String Structure
 MoV BL, 0ch; Character properties displayed
 MoV ah, 13 HClearly call the 13h subroutine
 Msgstr: DB "Hello my OS! "
 Int 10 h
 Times 510-($-$) db 0; Repeated n times each filling value is 0
 DW 55aah
 JMP $ ; Continuously jump to the current position, which is an endless loop


Category: Assembly | add to souzang | share to I post bar | browse (281) | comment (1)

Previous Article: port scanning next article: big gray wolf compilation video notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.