Assembly language Learning Chapter fifth-[BX] and loop instructions

Source: Internet
Author: User

This blog series reference from << assembly Language >> third edition, author: Wang Shuang


1.[BX] and the description of the memory unit

[BX] is similar to the one we saw earlier [0], MOV ax,[0] means to put two bytes of memory address of ds:0 into Microsoft Dynamics AX. where [0] 0 represents an offset address.

Similarly, we have mov al,[0] meaning that the single-byte contents of the memory address of ds:0 are stored in AL. Then we can boldly infer that MOV ax,[bx] represents the two-byte content of the memory address that offsets the value in the BX register into AX, where the segment address is stored in the DS.


2. Descriptive notation about the definition: "()"

Later, we will use the symbol "()" to represent a memory unit or a value in a register. (AX) Represents a value in register ax, (AL) represents a value in register al.

(20000H) content that represents memory address 20000

((DS) *16+ (BX)) The content of DS is R2 in R1,BX. Then the formula represents the content at the R1*16+R2 memory address.

There can be three types of elements in "()": (1). Register (2) segment Register (3) memory address There are two types of data represented: Word and byte. How to differentiate a data type is based on the register type and the type of operation.

Examples of "()" Applications:


1.[BX]

Look at a few instructions:

MOV AX,[BX] Set the offset address to SA, the segment address is stored in the DS register by default, then the purpose of this directive is to pass the value at address Ds:sa to the AX register.

Interpreted with the "()" symbol as (AX) = ((DS) *16+ (BX))

mov [bx],ax The offset address is SA, the segment address is stored in the DS register by default, then the function of this instruction is to pass the Register AX value to the position of the offset address represented by [BX], and the "()" symbol is interpreted as ((DS) *16+ (bx)) = (AX)



2. Loop command

The loop instruction is a loop instruction, which is in the form of a loop designator, and when CX is not 0, it jumps to the label at the same time and Cx=cx-1, if the label is 0, executes downward. (CX defaults to loop counter)


The following implementation of a small program, with loop implementation 2^12, the code is as follows:


1. Marking

There is an instruction in the designator s where add Ax,ax is used to accumulate, and the label s represents the address of the instruction.


2.loop s

When the value in the CX register is not 0, it jumps to the label where the add Ax,ax is executed. When cx=0, loop s no longer jumps and executes the instructions behind the loops.


The loop framework is implemented with CX and loop as follows:

mov cx, number of cycles

Label: Code Snippet


Loop label

There is now a program function that needs to be implemented: Calculate the number in the ffff:0006 unit multiplied by 3, and the result is stored in the DX.

This program has three points of attention

1, the number in the ffff:0006 unit is a byte-type data. The range is 0-255, multiplied by 3 can be placed in the DX, do not worry about the range of DX

2, the number in the ffff:0006 unit is assigned to AX, and then (dx) = 0, with DX storage accumulated three times the result

3, because ffff:0006 is a single byte unit, ax is a word unit. How do I assign the value of the ffff:0006 unit to ax? It is only necessary to extend the high-order byte in Ax to 00h, such as 2CH in ffff:0006, then 002CH after the assignment to ax, although the length of the data is unequal, but the values are equal.


Note the point, 0ffffh why there will be a "0", when the 16 binary data is the highest bit of the letter should be preceded by a maximum of 0.

Below we are using MASM and link two instructions to compile the program to generate EXE files.


To load the Vpoet.exe with the Debug tool:


DS for 14E9 Cs:ip for 14f9:0000 point to the first instruction MOV ax,ffff

Now we use the U command to view the program contents.


You can see that the address at Loop S is 14f9:e2fc at this time the S designator in loop s becomes Address 0012, and when the loop is executed, then cx=cx-1, if CX is not 0, set the IP to the address of the S-label 0012 The CPU will execute the address 14f9:0 The 012 command add Dx,ax implements the overlay.


After the first three instructions are executed, the DS is set to FFFFH,BX set to 0006H at this point Ds:bx point to Ffff:6, then the Mov al,[bx] takes out the value at the Ffff:6 address, which shows the value 32H on the right.

Continue with the following two instructions:


After execution completes the replication to AX, the AX value is 0032H


Continue to perform MOV dx,0 and mov cx,3 to complete the initialization of accumulators and counters


Following the start of the cycle, you can see that CX decreases by 1 per cycle, while DX increases by one.

When the cx=0000 loop ends, the next return statement is executed MOV ax,4c00h and int 21h. At this point the entire program ends up handing the CPU controller to command.


5.4 Debug and Assembler compiler MASM different handling of instructions

1. In assembly language, if an instruction is to access a memory unit, the instruction must use the [...] To represent a memory unit if it is in the [...] The offset address of the memory address is given directly by the constant in the, then a segment register is prepended to "[]". Like what

MOV al,ds:[0] If not shown in the given segment register DS:, then mov al,[0] in assembly language is understood as MOV al,0

2. In assembly language, the offset address can also be stored in an ordinary register BX, such as

MOV bx,0

MOV AL,[BX] This is also allowed, where the segment register is default in DS


5.5 Segment Prefix

Assembly instruction MOV AX,[BX] The offset address is indicated in the Register BX, and the segment address is in DS by default, and we can also show the segment address in the instruction:

Like what

MOV AX,DS:[BX]

MOV AX,DS:[BX]

MOV AX,CS:[BX]

MOV ax,ds:[0]

MOV ax,ds:[0]

MOV ax,cs:[0]

Where Ds:cs:ss: called segment prefix


5.6 A safe space

In the assembly, when we change the contents of a memory address, we need to be very cautious, because the address may store important code or data, once overwritten or changed will cause the whole system to crash. In DOS, DOS and other legitimate programs generally do not use the 256-byte space of 0:200~0:2ff. So, using this space is safe, we can see whether this address is 0 under debug, if 0 proves that the memory segment is not used by DOS and other programs.



Use of 5.8-segment prefixes

Problem, we need to copy the data from the memory ffff:0~ffff:b unit to the 0:200~0:20B

The analysis is as follows:

1.0:200~0:20B is equivalent to 0020:0~0020:B, which is described in order for the destination address and source address offsets to start at 0

2. The replication process is implemented using loop loops

Initialization

X=0

Loop 12 times:

Data from the FFFF:X unit is fed into 0020:X (requires a register relay)

X=x+1
3. The offset of ffff:x and 0200:x in the loop x is deposited in BX

The code and comments are as follows:


Ok,only Stop here~~


Assembly language Learning Chapter fifth-[BX] and loop instructions

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: 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

  • 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.