[Original] Compilation Blind Posts 1

zhaozj2021-02-12  218

[Original] Compilation Blind Posts 1 by SSSA2000 Write this post purpose is just to let the classmates who have not gotten into the door, in fact, my assembly technology is very poor, and many I don't know, I know I know, I have to write, come so long. I have almost haven't sent original post, embarrassing!

Start the topic one, compile common basic statements

We don't talk from the statement, first look at an example. Code Segment Assume CS: Code In DB?; Defining Variable INBEGIN: MOV AH 01 INT 21H; call DOS interrupt, this interrupt is used to read a number ASCII from the keyboard, deposit SUB AL, 30; ASCII code - 30, deposit BL MOV BL, Al Mov AH, 01; read the contents of the second character INT 21H SUB Al, 30 Add Al, BL; Al; Al; Al; put the content of Al; MOV AH, 4CH INT 21H in IN; these two statements are calling DOS interrupts, effect: downtime, generally used for the end of the program Code Ends end begin

explain:

MOV: Data Movement Directive, Format: The number of MOV target operates. Put the contents of the original operands in an immediate operand. This MOV is very learned. Next special explanation of this MOV directive, the master should not read it.

SUB: subtraction instruction format: SUB D, SD-S and put the result in D, which is D = D-S

Add: add D, S with SUB is similar, d = D S

About the format of assembly language, (DOS)

Code segment assume ... Define variables ....... Begin: ....... code ends end begin

Segment and Ends must be paired, Segment and Ends are segments, format: Segment name Segment [Positioning Type] [Combination Type] ['Category']. . . . . . . . . . Segment name ENDS

The role of Assume directives tells the assembler that the relevant segment register will be set to the address register of which segment in memory, and the true load of the segment address value must also be completed by assigning an execution instruction to the segment register.

To talk about a few examples of definition variables: NUM DW 12 defines a DW type called NUM, the value is 12 ABC DB 10 defines a DB-type variable called ABC, and the value is 10WORKER DD 1123 defines a named Worker. DD type variable, value is 1123

DB: byte variable DW: word variable, (a word equal to 2 bytes) DD: double word variable DQ: 4 word DT: 10 words

It's so tired, tomorrow, I have to go to the mechanical design class and software foundation. I can see God, (practicing fitness systemic pain) Finally, there are two instructions in the input and output in the usual assembly. Common depiction: MOV AH, 01 INT 21h, the second one is called system call, AH = 1 Description Call is the first function, this interrupt is to read a number ASCII from the keyboard, deposits in Al, (note that I don't write wrong, is in Al Ok, if you have any needs, tell me.

Compilation Tutorial 2 by sssa2000

Let us start. Last time I use an example to give everyone a blurred concept, maybe you don't fully understand the way to go to the dragon, this doesn't matter, there is a knowledge, you can.

I think that the biggest obstacle to learning compilation is that the teaching method in the book is not suitable for beginners. Even if there is a foundation, it is a bit confused. Since it is to start from the actual start, we will start our example warm up, and The last time is almost

============================================================================================================================================================================================================= =====================================================================================================================================================

OBJECTIV: To save the multi-character (note that multi-character) entered (note is multi-character) to the variable inbuf composed of several memory cells to end the carriage return.

Code segment

Assume cs: code; no data segment is required because there is no declaration

Start: MOV BX, 00h

Again: MOV AH, 01H; call the interrupt, don't say it, type a character to be ASCII to Al (note is a character)

Int 21h

CMP Al, 0DH; if the value in Al is 0DH,

JZ Stop; turn it to the STOP label

MOV [Inbuf BX], Al; puts Al's data to the address of Inbuf BX

Inc bx; bx = bx 1

JMP Again; jump to the Again Number to accept the next character until the user carries

Stop: MOV AH, 4CH

Int 21h

Code ends

End Start

============================================================================================================================================================================================================= ===================================================================================================================================================================================================== I am also afraid, people are lazy.

For this program, talk about MOV, and branch procedures.

Pay attention, background music is good, it is recommended to use my mystery, why? Because I am listening.

MOV instruction

Format: MOV destination operand, original operand

Explanation: 1. Immediately, the paragraph register CS cannot be used as a destination operation;

2. The original operands and destination operands cannot be the same as the number of memory operands. The following instructions are wrong

MOV 4H, BL

MOV CS, AX

MOV [1000H], [BX]; [] indicates the address

3, the number of original operands and destination operands must be consistent, the same as the number of single-by-thirds, or the same number of double-bytes, when there is a type of operand in the instruction, the other operand is considered as the same Types of. You can define a memory operand as byte or word type with Byte PTR or Word PTR. When the memory operand is a word type, the register or immediately high byte corresponds to its high address, the low-byte corresponds to the low address.

Explain that byte ptr is the force defined by an operand as a BYTE type, Word PTR is similar, similar to C language ()

What does the last sentence mean? We know, one word = two bytes, in the computer, the storage of the word is in bytes, everyone knows the stack to follow the principle of "advanced", so it is necessary to change the high and low. Example: A word type, the value is: 1234h, in memory location:

| 34h | 34 is the low position of the variable.

| ------- |

| 12H | 12 is the high of the variable.

| --------

| | |

Ok, let's see this sentence. "When the memory opera is a word type, the register or the high-character high byte corresponds to its high address, the low-byte corresponds to the low address." For example,

MOV [2000H], AX

Representation:

| Memory |

| ----------

| 存 a 的 低 低

| ----------

| High AX of AX AH

| ----------

Branch program

It is if ... Then, Goto, etc. Of course, in the assembly, there is no such statement

Let's take a look at the CMP instruction.

Format: CMP destination, original operand

Role: According to the size relationship of the two operands, the status flag value is generated.

JC / JNC Directive

Format: JC / JNC label

Role: JC has a carry, that is, CF = 1 JNC reverse (what is CF? I will specifically explain the computer's register, don't worry) JP / JNP

The number of JP: 1 is even if the number is turned (pf = 1), the JNP is opposite

JZ / JNZ

JZ: Turn to zero, (zf = 1) JNZ: opposite

JL / JGE

JL: Small than turning (sf <> of or zf = 0) JGE is greater than or equal to turn (sf <= of or zf = 1)

. . . . . . . . There are still many oh, don't write, check your information, don't be too lazy.

There is also one of the most important, JMP unconditional jump instructions, don't explain it?

That example is to look at it, I realized how to jump. Will be harvested ~~

Not addiction? Go another process,

============================================================================================================================================================================================================= =====================================================================================================================================================

Seeking absolute value of X-Y

DSEG segment

X dB 40h

Y db 73h

Z DB?

DSEG Ends

SSEG segment stack; setup stack segment

DB 80H DUP (0)

SSEG Ends

CSEG segment

Assume DS: DSEG, SS: SSEG, CS: CSEG; Is it feeling different from previous? Oh, realize

Start:

MOV AX, DSEG

Mov Al, X

CMP AL, Y; Generate Status Sign according to X-Y

JL XL; Sign number is jumping

SUB Al, Y;

MOV Z, Al; Z = X-Y

XL: MOV BL, Y

SUB BL, Al

MOV Z, BL; Z = Y-X

OK:

MOV AH, 4CH

Int 21h

CSEG Ends

End Start

============================================================================================================================================================================================================= ===========================

Ok, should there be no problem? I still want to talk about some addressing methods. The next chapter needs to tell the basic knowledge, avoid can't avoid it, but now, your understanding of the compilation should be a bit deep, and the compilation is not difficult, is it? If you have this feeling, then my purpose is also reached, and next, we explain the registers, and addressed. Do not speak. Ok, Class over! Compilation Tutorial 3 - Preparation for Upgrade by SSSA2000

After studying the two tutorials, I should have a profound impact on the assembler? So can't teach blind posts, you can upgrade, but before upgrading, you should not help some things about some things.

First, the internal register of the CPU:

4 16-bit general registers:

_____________________________

| __AH ________ | ____ al _________ | AX

| __BH ________ | ___cl _________ | bx

| __CH ________ | ____cl _________ | cx

| __DH ________ | ____d _________ | dx

Among them: In general, AX is used as accumulators in the multiplier method, and the BX can store the address, or the pointer register.

The CX can store the number of cycles, and the DX can store the I / O number.

AH is two 4 digits, Al is two 4 digits, so AX is 16 bits, and the rest is pushed

4 16-bit pointer registers

_____________________________

| BP base pointer

| ____________________________

| SP stack pointer for doing a stack top pointer

| ____________________________

| Si source address register

| _______________ These two sometimes used to do an index, store data, and operation results

| DI target register

| ____________________________

4 16-bit internal registers:

_____________________________

| CS: Code Segment Register

| ____________________________

| DS: Data Section Register

| ____________________________

| ES: Additional Segment Register

| ____________________________

| SS: Stack Section Register

| ____________________________

Sign Register:

____________________________

IP instruction pointer |

___________________________ |

Flagsh | flagsl |

_____________ | _____________ | status flag

The 16-bit flag register uses only 9 of them, and the 6 status flags, and 3 control flags.

FH:

_________________

| OF | DF | IF | TF |

___ | __ | __ | __ | __ |

FL:

___________________________

| SF | ZF | Empty | AF | Air | PF | Empty | CF |

| __ | __ | _ | __ | _ | __ | __ | __ |

The specific one means I simply tell, if you want to program, you have to look at your book.

CF: Ground sign, carry / borrowing, cf = 1

PF: Parity, when the low 8 bits of the command execution result contains even numbers, PF = 1, f Otherwise 0

AF: Auxiliary carry mark, the low byte of the result of the addition and subtraction method is 4-bit, and the AF = 1.ZF: 0 logo. When the calculation result is 0, ZF = 1

SF: Symbol flag, the highest level of his and calculation results. The negative number is 1, the positive number is 1

Of: overflow flag, complement operation is 1

The above is the status flag, below is the control mark:

DF: stepping direction of directional sign, control data string operation instruction

IF: Interrupt Allow Sign

TF: Track mark, setting up for debugging, people who like cracking should pay special attention to this

Second, addressing the way:

This thing is not guilty, there is no way, it is difficult to understand, I try to say clearly!

1, fixed addressing

This way, the operand is hidden in the instruction, for example, DAA, the number of operands is hidden in Al

2, immediately addressed

Used to represent a constant, for example:

MOV AX, 9; 9 is immediate

MOV AX 1234H; 1234h is immediate,

3, register addressing

For example: MOV AX, CX

Pre-execution AX = 9602H, CX = 2081H

After execution, AX = 2081H.

4, memory addressing

In this way, the operand is typically a data segment, a stack segment, a storage unit in an additional segment other than the code segment, an address or expression of the memory cell given by the instruction. In general, the number of destination operands and the original operand cannot be the same as the memory operand. Memory addressing is divided into five types:

Direct addressing: The valid address of the operands are given by the instructions, which is a constant or variable with square brackets. Physical address PA = 16 * (DS) offset address NN

E.g:

MOV Al, [1000H] transmits the content of the 1000H unit of the DS segment to Al, pay attention to the difference between square brackets and non-plus brackets.

MOV AX, [1000h]

The content of 1000H of the DS segment is transmitted to AX, pay attention to the contents of AH, Al. In addition, don't ignore physical addresses, this is very important information, cracking or programming, you must take this thing.

MOV Al, ES: [2000H] indicates that the contents of the ES segment is passed to Al, which is called the prefix.

Register indirect addressing:

Format: [BX, BP, Si or Di]. Address by the base address or the index register

If BX, Si, DI is used in the instruction, using DS as a segment address

PA = 16 * (DS) (BX / Si / Di)

For example: MOV Al, [BX] sets BX content to 1000h, then pass the content of the DS segment to Al

If you use BP, use the SS address

PA = 16 * (SS) (BP)

This addressing method is typically used to operate the value of the one-dimensional array of registers, change the registers such as BX.

I will be a bit complicated in the future, pay attention,

Attachment address:

Format: Offset [BX or BP]

For example: MOV Al, 80H [BP] set BP content = 2040h, transmit the contents of the 20c0 unit of the stack segment to Al

why? Because the 2040h 80H = 20c0h is not believed in the Windows Calculator in the Windows Calculator. In fact, this statement is equivalent to MOV Al, [80H BP]

Effective address EA = (bx) / (bp) offset

Address addressing

Format: Offset [Si or Di]

Effective address EA = (Si) / (DI) offset

Is it almost the same?

Let's take an example of an operator:

MOV AX, Array1 [Si]

Mov Array2 [DI], AX

Do you understand? Why can't Mov Array2 [DI] Array1 [Si]? Because the two operands cannot be variables. Don't worry, here I have not given physical address of these two ways, and I will give you a topic, it is easy, think about it!

Attached to address

In fact, it is the two combinations.

Format: Offset [BX / BP Si / Di]

PA = 16 * DS (BX / BP Si / DI offset)

Or 16 * ss (BX / BP Si / DI offset)

Note that BX and BP selection are different, and the segment address will be different. The former uses DS, the latter uses SS.

Ok, it's so much, it's a little headache. If you don't understand, I will reply, I have to draw, I stole it.

The beginner will slowly look, read more, don't think about it, you have a confidence! Next chapter we speak circulating statements, ok, go to sleep, maybe it is to name tomorrow.

Compilation Tutorial 4 - learn to solve problems

Really busy recently, it is C #, and it is Java, Robocode, and Win32ASM.

I am too busy, but the tutorial can't be delayed. After all, I wrote, I have to encourage me.

Very content.

I don't know how you look like it? I don't understand, I don't have the relationship. I will experience it. I will see a lot of registers, signatures, and I still spend a lot of time.

Question.

First, loop structure:

1 cycle instruction.

Format: loop command

Function: minimize the value of the register CX 1. Then it is determined that if CX <> 0, this transfer command to the label. Otherwise follow-up instructions continue. Equivalent to the following two instructions:

DEC CX

JNZ command mark

So first determine the number of cycles before the cycle, place in CX.

Note: Do not change CX in the cyclic body, as for the reason I don't have to say more.

Come to a program:

There are 10 data, 12, 0, -6, 44, -54, 0, 3, 51, 98, and 69 statistics of the variable VAR.

Now we have been analyzing, unlike previous key understanding statements.

10 data, each occupied one byte, then how do we implement data statistics? A cycle should be used. There are a few questions:

How to read data one by one? From a variable.

How is the number of statistics after comparing?

Will you repeat statistics?

Solved this procedure this program will come out.

The first question, for the readings of multiple data in a variable, the previous chapter, this is equivalent to an array, the most common method is to use BX to address.

Second question, use a special register, count 0

The third problem is actually very simple. The key is to mix this procedure and the algorithm of the bubbling method. This program is available for all the data.

OK, write planning, this is very important, just learning is best to write, especially compilation, decent register error. It's hard to find out.

Al: Save data read from variables

BX: Indirect addressing

CX: cycle

DL: The number of counters and statistics.

Data segment

VAR DB 12, 0, -6, 44, -54, 0, 3, 51, 98, 69RESULT DB?

Data ends

Code segment

Assume CS: Code, DS: Data

Start:

Mov AX, DATA

MOV DS, AX

MOV BX, 0

MOV DL, 0

MOV CX, 10; one pile of MOVs is for initialization

Again:

MOV Al, [BX VAR]

CMP Al, 0

JNZ LAB; if it is not equal to 0, turn LAB

INC DL; if the number of reads is 0, DL

Lab:

Inc BX

Loop again

Mov Result, DL

MOV AH, 4CH

Int 21h

Code ends

End Start

IT IS A Easy Job, ISN '?

Here is a problem, because I said before, 10 data, each occupied one byte, why don't the loop count does not have CL or CH? This will not waste, don't forget, the default operation of the loop command is CX, cannot be modified.

When designing programs, pay more attention to the use of registers, beginners tend to use it to use it.

Below, a procedure for bubbling method, I don't explain, I don't understand.

Data segment

VAR DB -1, -10, -100, 27H, 0AH, 47H

n equ $ -var

Data ends

Code segment

Assume CS: Code, DS: Data

B Mov Ax, Data

MOV DS, AX

MOV CX, N-1

MOV DX, 1

Ag: Call Subp; Calling Subproof

INC DX

LOOP AG

MOV AH, 4CH

Int 21h

*************************; the subroutine starts

Subp Proc

PUSH CX

MOV CX, N

SUB CX, DX

MOV Si, 0

RECMP: MOV Al, Var [Si]

CMP Al, Var [Si 1]

Jle Noch

XCHG Al, Var [Si 1]

XCHG Al, Var [Si]

Noch: Inc Si

Loop Recmp

POP CX;

RET

Subp endp

**********************************

Code ends

END B

It's a bit difficult, but I will see it will want to pass, consolidate, it will be deeper in the future.

2003 November 29, 1:33

SSSA2000

转载请注明原文地址:https://www.9cbs.com/read-6362.html

New Post(0)