Note: It should be a bug in Sina's blog: % added before "define" is garbled and can only be replaced with % 100. Therefore, do not be scared when 100define is shown below.
Certificate -----------------------------------------------------------------------------------------------------------------------------------------
I have seen Chapter 1 "protection mode". Reading the "eye-catching" pmtest1.asm repeatedly is still confused. I don't know much about the SECTION, BITS, and other keywords, so I need to be familiar with nasm compilation.
"A lot of people may use MASM when learning assembly." The author guessed it too well. What I learned was Wang Shuang's book (there were too few writers like China ). Yu Yuan mentioned the concept of "learning cost" and agreed with it very much. AT&T and INTEL-style Assembly statements eventually turn into the same machine code, so it is enough to learn the same, but it is a compilation tool.
The so-called "get familiar with nasm assembly" means to use nasm to write some basic subprograms and gradually learn about it. At the same time, it also accumulates a self-compiled sub-library, just like libc.
1,100 define plus (a, B) (a) + (B ))
Macros in this format are similar to those in C. They are pre-defined and used as follows: mov ax, plus (1, 2)
Nasm's macro processor translates it into: mov ax, 3 Accurately translated into "mov ax, (1) + (2 ))"
As for (a) + (B) Why should we add so many parentheses to prevent variable a and variable B from being computation, and add brackets to ensure macro substitution, the operation order is not disrupted.
2. What does org do?
Wang Shuang's book does not mention org, but there are some in the Yellow Book of Tsinghua University (like most books in China, the book is not intended for self-scholars at all ).
Bytes --------------------------------------------------------------------------------------
InNASMThe significance of ORG is illustrated in the BIN format program of assembly language assembly.
ORG 100 H
START:
Movax, START; This is equivalent to mov ax, 100 h
......
There is a START label above. When mov ax and START are executed, ax = 100 h. If ORG 100 H is not written, it is equivalent to or1_h. In this case, ax = 00 h.
That is to say, the value referenced by the address = the offset of the address in the file + the value of org.
In essence, the org Command tells the compiler in advance: "The code segment you are compiling will be loaded to the XXX address of the memory in the future ". The compiler calculates the physical address corresponding to each label. (I added this sentence myself)
Bytes --------------------------------------------------------------------------------------
The split line is very clear, from http://hi.baidu.com/chinfs/blog/item/535d0eed42866addb21cb18d. HtmlI just picked the beginning of the article. It is enough to explain the meaning, and there are some limitations in the second part of the original article.
3. Write the most primitive echo
The multi-row macro of nasm is very powerful. Now it is used to implement the most primitive echo function, displaying a string of characters in one row and one column on the screen. To facilitate reuse, the code is written to the echo. mac header file. You can simply add % include "echo. mac" to the code.
----------
% Ifndef echo_mac This is similar to c. It is also used to prevent repeated inclusion of header files.
100 define echo_mac
% Include "esbp. mac" This is another multi-row macro I have written to map the tag address to the corresponding es and bp registers.
% Macro echo 1
Jmp % start Nasm is really user-friendly: % is a local label defined specifically for multi-row macros, that is, you call this macro n times, nasm will
% Start is converted to n Different tags. If n + 1 is called, n + 1 different tags are converted. Imagine not having this function
If the same macro is expanded in n positions of a piece of code, it will get n identical % start, which is terrible.
% Local: db % 1
% Strlen bytenum % 1 The C language is -- # define bytenum strlen (% 1)
% Start: movcx, bytenum ; Start to int 10 h interrupt fill ParametersCx stores the length of the string to be displayed, that is, several bytes.
Movah, 13 h ; 13 H sub-function of int 10 h interrupt: displays the es: bp string pointed to in the specified Column
Moval, 1 h ; Al sets the display output mode. 1 indicates that the string contains only the display characters, and its display attribute is bl. After the display, the cursor position changes.
Mov dh, 0 ; 0th rows
Movdl, 0 ; Column 0th
Movbh, 0 ; 0 is the display page
Movbl, 00000100b ; Red characters
Esbp % local ; % Local is the memory address of the string. Use esbp macro to map it to es, bp register
Int 10 h
% Endmacro
% Endif
----------
This is the esbp. mac file:
----------
% Ifndef esbp_mac
100 define esbp_mac
% Macro esbp 1
; The address passed here must be a 16-bit digit, namely not greaterthan 0 xffff
Push ax
Push dx
Mov ax, % 1
Mov dx, % 1
And dx, 0x000f
Shr ax, 4
Mov bp, dx
Mov es, ax
Pop dx
Pop ax
% Endmacro
% Endif
----------
How is the result? The following test file boot. asm
----------
% Include "echo. mac"
Org 0x7c00
Start: echo 'Hello! Oranges world'
Times 510-($-start) db 0
Dw 0aa55h
----------
Compile it into a binfile and dd it to the mbr and bochs of the virtual floppy disk for debugging,
The echo macro in the Stone Age is ready!
4. The echo function is slightly enhanced to display strings to the specified columns. Below is echoAtLC. mac
----------
% Ifndef echoAtLC_mac
100 define echoAtLC_mac
% Include "esbp. mac"
% Macro echoAtLC 3
Jmp % start
% Local: db % 1
% Strlen bytenum % 1
% Start: mov cx, bytenum
Mov ah, 13 h
Mov al, 1 h
Mov dh, % 2
Mov dl, % 3
Mov bh, 0
Mov bl, 00000100b
Esbp % local
Int 10 h
% Endmacro
% Endif
----------
5. Make echo stronger so that it can display the string to the position of the cursor.
The idea is: Use the number 3 sub-function of int 10 h to obtain the row and column where the cursor is located, and then call the echoAtLC macro. Below is echoUnderCursor. mac
----------
% Ifndef echoUnderCursor_mac
100 define echoUnderCursor_mac
% Include "echoAtLC. mac"
% Macro echoUnderCursor 1
; Get the line & column cursor locate
Push dx
Push cx
Push ax
Push bx
Mov ah, 3 h
Mov bh, 0
Int 10 h ; Dx changed, and it storesline, column
Pop bx
Pop ax
Pop cx
EchoAtLC % 1, dh, dl
Pop dx ; Dxrecovered
% Endmacro
----------
6. readCursor_D.mac and setCursor_page_line_column.mac
The Macros in the two header files are used to read the cursor information and set the cursor position, that is, the packaging of the functions of the 3 and 2 sub-functions of the int 10h
Header file readCursor_D.mac
----------
; Change dx
; Dh store line, dl store column
% Ifndef readCursor_D_mac
100 define readCursor_D_mac
% Macro readCursor_D 0
Push bx
Push ax
Push cx
Mov bh, 0
Movah, 3 h The exit parameters of sub-function 3 are placed in cx and dx respectively. I cannot use the information in cx, so pop overwrites
Int 10 h
Pop cx
Pop ax
Pop bx
% Endmacro
% Endif
----------
Header file setCursor_page_line_column.mac
----------
; Change no reg
% Ifndef setCursor_mac
100 define setCursor_mac
% Macro setCursor_page_line_column 3
Push ax
Push bx
Push dx
Mov bh, % 1
Mov dh, % 2
Mov dl, % 3
Movah, 2 h The subfunction 2 is used to set the text coordinates of the cursor.
Int 10 h
Pop dx
Pop bx
Pop ax
% Endmacro
% Endif
----------
The more header files are written, the more unified the specification I started to pay attention to. For example, the first line of comment indicates whether the Macro will affect the register after execution. If yes, add the _ D suffix after the macro name, for example, readCursor_D. _ D indicates the macro dangerous. For example, if the macro requires more than two parameters, I will append the parameter name information to the macro name, such as setCursor_page_line_column. In this way, I will know which parameters to enter. For example, the macro name should be the same as the name of the header file. (Of course, it does not include the. mac suffix)
7. Implement the carriage return function of the cursor cursorEnter. mac
----------
; Change no reg
% Ifndef cursorEnter_mac
100 define cursorEnter_mac
% Include "setCursor_page_line_column.mac"
% Include "readCursor_D.mac"
% Macro cursorEnter 0
Push dx
ReadCursor_D ; Get the row and column information of the current cursor
Incdh ; Add 1 to the row value of the cursor to implement line feed.
SetCursor_page_line_column 0, dh, 0 The last parameter 0 is used to place the cursor in The 0th column and press ENTER ----->.