Experiment 4 of assembly language Wang Shuang Fourth Edition (including detailed explanation of [bx] and loop)

Posted by akabugeyes on Tue, 04 Jan 2022 21:05:14 +0100

Basic concepts

All experimental addresses of assembly language

  1. [bx] and memory unit
[bx]And[0]similar,[0]Represents the memory unit, and the offset address is[0],The segment address defaults to ds
 example: mov ax,[0]    ->Send the contents of a memory unit into ax(The length of memory unit is 2 bytes, which mainly depends on ax (2 bytes)
	 mov al,[0]    ->Send the contents of a memory unit into al(The length of memory unit is 1 byte, which mainly depends on al (1 byte)
	 The above segment addresses are taken by default ds In, the offset address is taken as[0]in
	
    mov ax,[bx]    ->Same as above, except that the offset address is stored in[bx]in
    mov al,[bx]    ->Same as above, except that the offset address is stored in[bx]in
  1. loop
In Experiment 1, we find 2^8 Is to use the cycle
  1. Descriptive symbol "()"
()Used to represent the contents of a register or memory unit
(ax)express ax The content of,(20000H)Represents 20000 H Content of the unit
 The addresses of memory units are physical addresses
()The element in can be 1.Register name 2.Segment register name 3.Physical address of memory unit
 Cannot be, for example (2000):0)Such (segment address: in the form of offset address), if you want to describe it, it can be((ds)*16+(bx))
  1. The Convention symbol idata represents a constant

[BX]

mov ax,[bx]
As can be seen from the above description, the meaning of the instruction is(ax) = ((ds) * 16 + (bx))    ->bx Is the offset address, ds Is the default segment address

Loop instruction

loop Format of instruction: loop grade
CPU implement loop Instruction: 1.(cx) = (cx) - 1
			    2.judge cx Whether the value of is 0. If it is zero, exit the loop
 Frame:
	mov cx,Number of cycles
s:
	Loop executed program segment
	loop s
 In fact, on the whole while(num--){Execution procedure}The cycle is almost the same,num Is the number of cycles, minus one each time. If it is not zero, execute the program
 give an example:
	Add 123*236,The results are stored in ax in
 Analysis: using addition is 123 plus 236 times
	assume cs:code
	code segment
		mov ax,0
		mov cx,236
	s:
    	add ax,123
    	loop s
    	
    	mov ax,4c00h
    	int 21h
    code ends
    end

Tracking loop programs implemented with loop instructions in Debug

calculation ffff:0006 The number in the cell is multiplied by 3, and the result is stored in dx in
assume cs:code
code segment
	mov ax,0ffffh	;Assembly data cannot start with a letter, so prefix 0 is added
	mov ds,ax
	mov bx,6
	
	mov al,[bx]
	mov ah,0
	
	mov dx,0
	
	mov cx,3 		;Set the number of cycles
s:  add dx,ax
	loop s
	mov ax, 4c00h
	int 21h
code ends
end


Just follow the steps in the book

Debug: g instruction

When we were Debug If we want to track a specific program segment instead of the previous program segment, we can use g instructions
 For example, we want to start from cs:0012 Position start tracking ->  g 0012
 After executing the above instructions, from cs:ip Position start execution to cs:0012 Stop the position and wait for your command...

Debug: P command

When we want the loop to be executed at one time, we can't press it all the time t OK, you can use it p Instruction execution cycle
 When we meet loop Use in command p The instruction can execute the loop at one time

Different processing of instructions by Debug and masm

The difference between the two is mainly for[ idata]Explanation of
 stay Debug in mov al,[0]It means to ds:0 Data in is sent to ax in
 In the assembly source program, it is interpreted as: send data 0 to ax in

So what should we do in the source program ds:0 Content in ax Where are you?
1.Can use[bx]Register as transition
mov ax,2000h
mov ds,ax
mov bx,0
mov al,[bx]
2.stay[0]Plus the segment address shown in front of
mov ax,2000
mov ds,ax
mov al,ds:[0]

Joint application of loop and bx

Problem: calculate the sum of data in cells ffff:0 to ffff:b, and store the result in dx

Analysis: can the data of ffff:0~ffff:b be directly accumulated? No, because the data is 8 bits, it cannot be directly added to the 16 bit register dx

Can I add data to dl and set (dh) to 0? No, 12 8-bit data accumulated into an 8-bit register may cause carry loss

resolvent:

​ 1. Use a 16 bit register as a transition, and then accumulate the 16 bit data into dx in turn

The code is as follows:

assume cs:code 
code segment 
	mov ax,0fffffh		;Data cannot start with a letter, so it is prefixed with 0
	mov ds,ax
	mov bx,0			;bx Represents the offset address
	mov dx,0		
	mov cx,12			;A total of 12 cycles
	
s:  mov al,[bx]
	mov ah,0
	add dx,ax
	inc bx				;bx Plus 1
	loop s				;loop

	mov ax,4c00h
	int 21h
code ends
end

Segment prefix

It is the segment prefix that appears in the access address unit and is used to display the segment address indicating the memory unit, such as "ds:" "cs:"

A safe space

  • We need to write directly to a piece of memory
  • This memory should not store the code or data of the system or other programs
  • In Dos mode, there is generally no data and code of the system or other programs in the 0:200~0:2ff space
  • When writing content to memory, use a space of 0:200~0:2ff

Use of segment prefix

Copy the data in ffff:0ffff:b unit in memory to 0:200020b (equivalent to 0020:0~0020:b)

We cyclically store the data in f... In dl (one byte unit is OK), and then transfer dl to 0020:

assume cs:code
code segment 
	mov bx,0
	mov cx,12		;Cycle 12 times

S:	mov ax,0ffffh
	mov ds,ax
	mov dl,[bx]			;At this time, a unit of data in the original memory is placed in the dl Yes
	
	mov ax,0020h
	mov ds,ax			;replace ds The storage of is the segment address of the target memory
	mov [bx],dl			;take dl The contents of are transferred to the target memory
	
	inc bx				;bx Pointer plus one
	loop s


code ends
end

You can add a segment prefix to make the code more concise

assume cs:code
code segment
	
	mov ax,0ffffh
	mov ds,ax
	
	mov ax,0200h
	mov es,ax
	
	mov bx,0
	
	mov cx,12

s:  mov dl,[bx]		;take ds:bx Save in dl in
	mov es:[bx],dl
	
	inc bx
	loop s
	
	mov ax,4c00h
	int 21h

code ends
end

Experiment 4: the use of [bx] and loop

  1. Transfer data 0 ~ 63 (3FH) to memory 0:200~0:23F in turn

    Analysis 1: similar to the above problem, we need to transfer data 64 times (from 0 - 63) and transfer it with an 8-bit register each time

    😃

assume cs:code 
code segment 

	mov ax,0
	mov ds,ax		;Segment data is 0
	mov cx,64		;64 Secondary cycle
	mov bx,200h
	mov al,0
	
s:	mov [bx],al
	inc bx			;bx add one-tenth
	inc al			;Value plus one

loop s
	
	mov ax,4c00h
	int 21h
	
code ends
end

Let's debug after compiling the link to see the memory of 0:200

It is found that the memory is indeed filled from 0-63 (0-3F). Those who are not familiar with compiling link debug can see my last article with detailed steps

  1. Data 063 (0~3FH) is successively passed into memory 0:2000:23F. Only 9 instructions can be used in the program, including mov ax,4c00h and int 21h
assume cs:code

code segment

	mov bx,0h
	mov ds,bx
	mov ax,200h
	mov cx,64
	
s:  mov [bx+200h],bx
	inc bx
	loop s
	
	mov ax,4c00h
	int 21h

code ends
end

The result is no longer demonstrated. You can debug it yourself

  1. Complete the following program. Its function is to copy the instructions before mov ax and 4c00 to memory 0:200, complete the program, debug on the computer and track the operation results

    Tips:

    1. What did you copy? From where to where?
    2. What is copied? How many bytes are there? How do you know the number of bytes to copy?

We copy instructions, which are stored in cs: ip, so the first blank should be filled in cs

The second blank is filled in 17. You can check the number of bytes occupied before execution

assume cs:code
code segment

	mov ax,cs
	mov ds,ax
	mov ax,0020h
	mov es,ax
	mov bx,0
	mov cx,23
s:  mov al,[bx]
	mov es:[bx],al
	inc bx
	loop s
	
	mov ax,4c00h
	int 21h
code ends
end

After we finish the execution, check it with debug

Topics: Assembly Language Assembly