This chapter mainly describes what is [BX] and loop (loop) instruction how to use, loop and [BX] how to combine, what the paragraph prefix is what ghosts, and how to use the paragraph prefix. 1, [BX] concept [BX] and [0] similar, [0] indicates that the memory unit offset address is 0. To fully describe a memory unit, you need two kinds of information: the address of the memory unit, the length of the memory unit (type). [BX] also represents a memory unit, its offset address in BX, such as the command: mov ax,[bx]. Here we take a program as an example:
1AssumeCS:CODESG2 CODESG Segment3 Start: movax,2000h4 movDs,ax5 moval,[0]6 movbl,[1]7 movcl,[2]8 movdl,[3]9 movax,4c00hTen int21H One CODESG ends AEnd Start
View Code Compile, connect, debug this program with Debug. For the compiler, the brackets in [0] do not exist, so a 0 is placed in AL and 1 is placed in the BL. So, if we want to offset the address to a certain number, the corresponding memory unit is passed into the register, it takes two steps: mov bx,0 mov ax,[bx] & nbsp To do so, it is possible to pass the value of the memory cell corresponding to the offset address of 0 to the register. 2, loop This is a circular operation, instruction format: Loop label.  CPU Two steps to perform the Loop command: (1) (CX) = (CX)-1 (where CX is placed in parentheses, meaning CX values) (2) determines if CX is zero, not zero, and goes to the label execution program. Therefore, loop and CX are directly linked, that is, the value of CX affects the results of the loop execution. So the value of CX is also refers to the loop number of loops, we can think of high-level language, when using loops (such as for loop), we will define a variable, as the condition of the loop, the value of this variable is the number of cycles. Now let's look at the code that uses the assembly to calculate 2^2 results:
1 CS: Code 2 Code Segment 3 mov ax,24 add ax,ax5 6 mov ax,4c00h 7 int 21h 8 code ends 9 End
View CodeThe calculation 2^3 is similar, the code is as follows:
1AssumeCS:Code2 Code Segment3 movAx24 AddAx,ax5 AddAx,ax6 7 movax,4c00h8 int21h9 code endsTen End One
View CodeSo the calculation 2^12, we have to use the loop to do, otherwise there are too many duplicate code, this time we will use the Loop command, the code is as follows:
1AssumeCS:Code2 Code Segment3 movAx24 movCx One5 S:AddAx,ax6 Loop S7 8 movax,4c00h9 int21hTen code ends OneEnd
View Code In the above code, S is the label, the label can be any of your own definition of the content, in fact, s marked an address, this address has an instruction: add Ax,ax. CX we define a value of 11, because to calculate 2^12, so we have to loop 11 times. In the loop, CX is reduced by 1, the cycle is reduced one time, until CX is 0, and the loop is stopped. We can use debug debugging to compile the connected program: from the above, we can get the CX and loop instructions with each other three key points: (1), the number of storage cycles in CX (2), the address identified by the Loop instruction label is in front (3), the program segment to be executed, written between the label and the loop instruction. Joint use of 3, loop and [BX] Let's start with a few questions: Calculate the ffff:0~ffff:b of the data in the cell and store the results in DX. (1), does the result of the operation exceed the range of values that DX can store? The data in the FFFF:0~FFFF:B memory Unit is byte-type data, and the range is 0~256,12 such that the result is no larger than 65535, so the DX can be stored. (2), can I add ffff:0~ffff:b directly to DX? No, because the data in FFFF:0~FFFF:B is 8 bits and cannot be added directly to the 16-bit register DX. (3), can the data in the ffff:0~ffff:b be added to the DL, and set (DH) = 0, so that the accumulation into the DX? No, DL is a 8-bit register, the range of data that can be accommodated is between 0~255, the data in FFFF:0~FFFF:B is 8 bits, and 12 8-bit data is accumulated into a 8-bit register, There may be a case where the rounding is lost. (4), so how exactly should the data in this piece of memory be stored in the DX? is using a 16-bit register to mediate, assigning 8 bits of data in a memory unit to a 16-bit register ax, and then adding the data in this ax to the DX, so that the type of the operand can match (ax, DX is 16 binary) and the result does not go out of bounds. Because there are 12 additions to the loop, the loop addition is used to add the following code:
1AssumeCS:codeseg2 codeseg Segment3 4 movAX,0FFFFH5 movDs,ax;Initialize DS:BX point to ffff:0 memory Unit6 movBx07 8 movDx0 ;Initialize the accumulator register DX9 movCx A ;Initialize the cycle count register CXTen One S: movAL,[BX] A movAh0 - AddDx,ax;indirectly add the values in the ((DS) *16+ (BX) unit to the DX - IncBx;DS:BX refers to a unit down the Loop S - - movax,4c00h - int21h + - codeseg ends +End
View Code4, Debug G command and P command when we debug the program, the number of cycles is very large, we can not execute the T command once, so use the G command or the P command. The offset address at which the G loop ends so that the contents of the loop can be executed at once. P, executes the contents of the loop directly. 5, a safe space when we want to write directly to the memory, this memory space should not be stored in the system or other program data or code, otherwise the write will cause the operating system error, DOS mode, the general situation, 0:200~ There is no data or code for the system or other programs in the 0:2FF, and we write the memory directly into the space. 6, paragraph prefix instruction mov AX,[BX], the offset address of the memory unit is given by BX, while the segment address by default in the DS, we can access the memory unit in the instruction, show the memory unit of the segment address is located in the segment register, for example, MOV ax,ds:[bx], The segment registers here can be ds,cs,es,ss, and the segment registers in these directives are also referred to as segment prefixes in assembly language. 7, the use of the prefix of the paragraph first we consider a problem: Copy the data in the ffff:0~ffff:b to the 0:200~0:20b cell. Analysis: (1) 0:200~0:20b is 0020:0~0020:B, which describes the same memory space. (2) in the loop, the source unit ffff:x and the target cell 0200:x offset address X is a variable, we use a register BX to store the code as follows:
1AssumeCS:codeseg2 codeseg Segment3 4 movBx0 ;offset address starting from 05 movCx A ;number of cycles 12 times6 7 S: movAX,0FFFFH8 movDs,ax;The value of DS is 0FFFFH9 movDL,[BX];(DL) = ((ds) *16+BX) The data in the FFFF:BX is passed into the DLTen One movax,0020h A movDs,ax;The value of DS is 0020h - mov[BX],DL;((ds) *16+BX) = (DL) The data in the DL is passed into the 0020:BX - the IncBx;BX self-increment 1 - Loop S - - movax,4c00h + int21h - + codeseg ends AEnd
View CodeOf course, we have the above code to improve the place, from the above code can be seen, we repeatedly set the value of the DS two times, it is not wrong, just in the case of a smaller number of cycles, once the number of cycles increased to very large, the CPU will have to do a lot of repeated operations, so the efficiency is relatively low. Therefore, we can make corresponding improvements to the code:
1AssumeCS:codeseg2 codeseg Segment3 movAX,0FFFFH4 movDs,ax;(DS) =0FFFFH5 6 movax,0020h7 movEs,ax;(es) =0020h8 9 movBx0 ;(BX) = 0, at this time, Ds:bx point ffff:0,es:bx refers to 0020:0Ten movCx A One A S: - movDL,[BX];(DL) = ((DS) *16+ (BX)), the data in the FFFF:BX is passed to the DL - mov es:[BX],DL;((es) *16+ (bx)) = (DL), the data in the DL is passed into the 0020:BX the - IncBX - Loop S - + movax,4c00h - int21h + A codeseg ends atEnd
View Code
Small Turtle 0 Basic assembly Language learning note Fifth [BX] and loop instructions