Assembly language Writing DOS memory-resident program (2)
Source: Internet
Author: User
Basic principle
2.1 8086/8088
The IBM PC Central Processor (processing unit) is a microprocessor inter 8088,8088 is a small version of the 8086. For writing programs, the two are almost identical. The difference between the two is: Their external communication. 8086 and the outside communication is through the 16-bit input and output channel, memory access is also a 16-bit each time, 8088 and 8086 very similar, but it and the outside world must communicate through 16-bit channel.
2.1.1 Registers
The 8086/8088 structure is simple and contains a set of 16-bit registers for general purposes. Ax,bx,cx,dx,bp,si,di. AX,BX,CX,DX can also be divided into 8-bit registers, such as: Ax can be divided into ah,al; BX can be divided into bh,bl; CX can be divided into ch,cl;dx which can be divided into dh,dl. The use of registers Bp,si,di is not particularly limited, but it cannot be divided into two bytes. Additionally, the Register SP is used primarily as a stack pointer. In addition, there are four very important segment registers (Segment register ): cs,ds,ss,es. Instruction pointer (instru-ction pointer) IP is used to control the current CPU execution to which instruction.
8086 is designed to be compatible with 8-bit CPU8080.8-bit computers are addressable using two bytes (that is, 16 bits), so their addressable space can be up to 64K bytes. 16-bit CPUs have a completely different approach to address setting. CPU in Segment (Segment) as the unit, Each range includes 64K bytes, while in memory it can contain many segments. Therefore, the operating system can be executed in one paragraph. The user's program can be executed in another paragraph. Within a paragraph, A package can think of a computer as having only 64K bytes of memory space. As a result, programs that were executed on the original 8-bit computers can be easily ported to 16-bit computers. In addition, memory segments can overlap each other, so two different programs can share a piece of memory. The segment values are set by registers, The actual address value is to move the segment value (16 bits) to the left 4 digits, the 16-bit displacement (offset) is then added, thus constituting a 20-bit address value. So 8086 can do a 20-bit address directly, that is, potentially accessing a megabyte of memory. In this megabyte of memory, IBM The PC retains the first 320K bytes to the system's ROM BIOS and display memory, so the user can use 640K bytes at most.
2.1.2 Addressing Mode
Addressing mode (addressing mode) is the key to many complex operations on a computer. 8086 provides the following addressing methods: Immediate addressing, memory indirection, register indirection, and so on.
Address immediately, using numbers directly.
Memory indirection, where values are stored somewhere in the data segment.
MOV Bx,foo
Foo DW 5
Register indirection. There are two registers that can be used under this addressing mode: the base register (base register) and the index register. The base address registers are BX and BP, and the index registers are Si and di. In this addressing mode, The register holds the address value in the data segment.
MOV ax,0f000h
MOV Es,ax
MOV Si,0fffeh
mov dl,byte ptr es:[si]
The above procedure uses the indirect addressing method, reads the data which is located at the F000:fffe position by the register Si. When the register is accessed indirectly, only one index register can be used for each of the MA base address registers.
The above addressing methods can do different combinations, so the results are many after the combination.
2.1.3 Logo
8086 has 9 one-digit flags (FLAG) that can be used to indicate the various states of the CPU. The following is a brief introduction to the 9 flags:
CF (Carry Flag): The CF is 1 o'clock to indicate that the result of the arithmetic operation exceeds the correct length.
PF (Parity Flag):P F is 1 means that parity is used, and PF 0 means using odd checksums.
AF (Auxiliary Carry Flag): It is the same as CF, except that it is used in low 4-bit results. AF is typically used on 20-bit address calculations.
ZF (zero Flag): ZF 1 means the result of the operation is 0, otherwise the ZF is 0.
SF (Sign Flag): The SF is 1 to indicate that the highest digit of the operation result is 1, otherwise the SF is 0.
TF (Trap Flag): TF executes as a 1,cpu on a single step, and a special interrupt occurs for each command completed in this mode.
if (Interrupt Enable Flag): If 1, allows the CPU to receive external interrupts, otherwise if 0.
DF (Direction Flag): This logo is used in circular instructions, such as: MOVS,MOVSB,MOVSW,CMPS,CMPSB and CMPSW. If DF is 1, the value of the address increases forward when the loop runs. If DF is 0, the address is reduced back.
of (over Flag): of 1, indicating that an operation that takes a positive or negative number exceeds the length of the correct byte.
2.1.4 Cycle
All loop instructions are in CX as a counter. A loop executes repeatedly until CX equals a particular value. The following procedure is to multiply the two numbers by adding them repeatedly.
MOV ax,0
MOV cx,4
Next:add ax,6
Loop Next
In the above program, the loop instruction executes with CX minus 1 and checks the contents of CX, and if CX equals 0, it moves to the next instruction, or jumps to the place marked next.
You can also use the following program to accomplish the same function:
MOV ax,0
MOV cx,4
Next
Add ax,6
Dec CX
CMP cx,0
Jne Next
Data structure of 2.1.5 memory
8088 is the base unit for accessing data in bytes. The computer's storage structure is a 8-bit byte, but the CPU itself handles the data in 16-bit units. In memory, we follow a principle, that is, how jagged is stored. High byte corresponds to high address, low byte corresponds to low address.
Here is a simple program that puts a byte in the ax and displays:
CSEG segment
Org 100h
Assume Cs:cseg,ds:cseg
Start
MOV Bx,cs
MOV ds,bx
mov ah, ' H '
mov al, ' L '
MOV Test,ax
mov Al,[si]; A test
Call Dchar
mov al,[si+1]; Second byte of test
Call Dchar
Ret
;D Isplay the character contained in AL
Dchar proc
Push AX
Push BX
MOV bh,1
MOV Ah,0eh
int 10h
Pop bx
Pop ax
Ret
Dchar ENDP
Test DW?
Cseg ends
End Start
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:
info-contact@alibabacloud.com
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