"Assembly Language" - Reading Notes - Chapter 7 - A More Flexible Method of Locating Memory Addresses

7.1 and and or instructions

  1. and instruction: Logical AND instruction, bitwise AND operation
mov al, 00001111b
and al, 01010101b
;结果: 00000101b  双方都为1的位为1,其他为0
  1. or instruction: logical or instruction, bitwise or operation
mov al, 00001111b
and al, 01010101b
;结果: 01011111b  只要有1方为1的位就为1,双方都是0的位为0

7.2 About ASCII code

The computer only recognizes it 01.
So people define a lot of rules, ASCIIthat is, the encoding rules of a text
, such as lowercase letters, define its encoding ain . So the process of reading and writing is:ASCII97
a

  • write a
    1. press the keyboarda
    2. The program receives coded aaccording to the rules, and getsASCII97
    3. Convert 97to binary 1100001and save. (memory or hard disk)
  • read a
    1. The program reads from the memory until 1100001the computer knows that this is decimal97
    2. Decoding using ASCIIrules, 97representing lowercasea
    3. Notifies the graphics card that it will abe drawn. (Introduced earlier is to write video memory, how to draw is the work of display)

It can be seen that the data itself is dead, and it is only meaningful if the program processes it according to specific rules.
If there is another program, which contains another set of rules, then when it reads it 1100001has a completely different meaning.

7.3 Data given in character form

In the assembly program, '......'character data is declared in the form of (single quotation marks), and the compiler will convert them into corresponding ASCIIcodes.

Procedure 7.1

assume cs:code, ds:data

data segment	;16字节
	db 'Jerry'	; 4A 65 72 72 97
	db 'abc'	; 61 62 63
data ends

code segment
start:	mov al, 'a'
		mov bl, 'b'
		
		mov ax, 4c00h		
		int 21h
code ends
end start

insert image description here
The compiler converts characters into ASCIIcodes.

7.4 Problems with case conversion

insert image description here

  1. Observe ASCIIthe code table (although otherwise you can observe the rules), ASCIIthe codes of uppercase and lowercase letters are related 32.
  2. When converting uppercase to lowercase, convert large to small + 32, and small to large -32. (But ASCIIthe design is very clever, we don't need to judge the current capitalization)
  3. AThe difference with ais the first 6place. 00100000b= 20H= 32
    01 0 00001
    01 1 00001

Use bit operations and 1101 1111to convert to uppercase. (The 6th position is 0, the other bits remain unchanged)

	  A:01000001  a:01100001
and	20H:11011111	11011111
-----------------------------
	  A:01000001  A:01000001

Use bit operations or 0010 0000to convert to lowercase. (bit 6 is 1, other bits remain unchanged)

	  A:01000001  a:01100001
or	20H:00100000	00100000
-----------------------------
	  a:01100001  a:01100001

insert image description here

7.5 [bx+idata] (variable + fixed offset)

idata: Indicates a constant.
mov ax, [bx+200]Indicates that ((ds)*16+(bx)+200)the data is fetched from the memory and sent into it ax.
[]The final result in is the physical address: bxcontent + constant 200. After getting the physical address, the content is taken out from the address.

Question 7.1

Write down the contents of ax, bx, and cx after the following program is executed

code segment				; ax	bx		cx		ds
start:	mov ax, 2000h		; 2000
		mov ds, ax          ; 						2000
		mov bx, 1000h       ;		1000
		mov ax, [bx]        ; 00BE
		mov cx, [bx+1]      ; 				0600			
		add cx, [bx+2]      ;				0606
code ends                   
end start

insert image description here

7.6 Use [bx+idata] to process arrays

idata: According to the understanding of address passing, idata is equivalent to the array address
bx: Indicates the amount of traversal, which is equivalent to i when traversing the array

Convert the first string defined in data to uppercase and the second string to lowercase.

assume cs:code;, ds:data

data segment	;16字节
	db 'jerry'
	db 'ABCDE'
data ends

code segment			
start:	mov ax, data	
		mov ds, ax  
		
		mov bx, 0			; i = 0		
		mov cx, 5			; len = 5
	s:	mov al, [0+bx]		; 0表示第一个数组的位置 bx表示索引
		and al, 11011111b	; 转大写
		mov [0+bx], al		; 送回内存
		
		mov al, [5+bx]		; 5表示第二个数组的位置 bx表示索引
		or  al, 00100000b	; 转小写
		mov [5+bx], al		; 送回内存
		
		inc bx				; i++
		loop s				; cx--; cx != 0 继续循环,循环结束
		
		mov ax, 4c00h
		int 21h
code ends                   
end start

insert image description here

7.7 SI and DI (source address > destination address)

siand are registers diwith similar functions in 8086CPU . si and di cannot be divided into two 8-bit registers for use.bx

  • A common usage is when copying data between two pieces of memory:
    ds:si points to the source
    ds:di points to the destination

Question 7.2

Use siand dito welcome to masm!!copy the string to the data area behind it.

assume cs:codesg,ds:datasg
datasg segment
	db 'welcome to masm!'	;16字节
	db '................'	;16字节
datasg ends

codesg segment			
start:	mov ax, datasg	
		mov ds, ax
		mov si, 0			; 指向第一行字符开头
		mov di, 16			; 指向第二行字符开头
		
		mov bx, 0			; i = 0	对应字符索引	
		mov cx, 8			; len = 8 每次复制一个字,共循环处理8次
	s:	mov ax, [si+bx]		; 取第一行的第bx个字符
		mov[di+bx], ax		; 送到第二行的第bx位
		add bx, 2			; i++ (每次偏移1个字 = 2字节)
		loop s				; cx--; cx != 0 继续循环,循环结束
		
		mov ax, 4c00h
		int 21h
codesg ends                   
end start

insert image description here

Question 7.3

Optimization problem 7.2 Reduce the amount of code.
It can be reduced by means of [bx+idata]OR or [si+idata]OR .[di+idata]

assume cs:codesg,ds:datasg
datasg segment
	db 'welcome to masm!'	;16字节
	db '................'	;16字节
datasg ends

codesg segment			
start:	mov ax, datasg	
		mov ds, ax
		mov si, 0			; i = 0
		mov cx, 8			; len = 8 每次复制一个字,共循环处理8次
	s:	mov ax, 0[si]		; 取第一行(偏移量0)的第bx个字符
		mov 16[si], ax		; 送到第二行(偏移量16)的第bx位
		add si, 2			; i++ (每次偏移1个字 = 2字节)
		loop s				; cx--; cx != 0 继续循环,循环结束
		
		mov ax, 4c00h
		int 21h
codesg ends                   
end start

insert image description here

7.8 [bx+si] Sum[bx+di]

bx+ variable, more flexible
For example, array operations:
si: indicates the original array (points to the address of the first element of the array in memory)
di: indicates the target array (points to the address of the first element of the array in memory)
bx: indicates the
index It is convenient to operate on two arrays in one cycle.

Question 7.4

Write down the contents of ax, bx, and cx after the following program is executed

; 2000:1000 BE 00 06 00 00 00
assume cs:codesg
codesg segment			    ; ax	bx		cx		ds		si		di
start:	mov ax, 2000H	    ; 
		mov ds, ax          ; 						2000
		mov bx, 1000H		; 		1000
		mov si, 0		    ; 								0000
		mov ax, [bx+si]		; 00BE
		inc si				;								0001
		mov cx, [bx+si]		;				0600
		inc si				;								0002
		mov di, si			;										0002
		add cx, [bx+di]		;				0606
		mov ax, 4c00h		
		int 21h
codesg ends                   
end start

insert image description here

7.9 [bx+si+idata] Sum[bx+di+idata]

bx+variable+constant, more flexible
For example, two-dimensional array operation: (through the offset, the concept of row and column is realized)
idata: represents a two-dimensional array (pointing to the address of the first element of the array in memory)
bx: represents a row (equivalent In the nested for, the i)
di of the first layer of for: represents an example (equivalent to the j of the second layer of for in the nested for)

Question 7.5

Write down the contents of ax, bx, and cx after the following program is executed

; 2000:1000 BE 00 06 00 6A 22
assume cs:codesg
codesg segment			    ; ax	bx		cx		ds		si		di
start:	mov ax, 2000H	    ; 
   	mov ds, ax          ; 						2000
   	mov bx, 1000H		; 		1000
   	mov si, 0		    ; 								0000
   	mov ax, [bx+2+si]	; 0006
   	inc si				;								0001
   	mov cx, [bx+2+si]	;				6A00
   	inc si				;								0002
   	mov di, si			;										0002
   	add cx, [bx+2+di]	;				8C6A(6A00+226A)
   	mov ax, 4c00h		
   	int 21h
codesg ends                   
end start

insert image description here

7.10 Flexible application of different addressing modes

  1. [idata]Use a constant to represent the address, which can be used for 直接定位a memory unit;
  2. [bx]Use a variable to represent the memory address, which can be used for 间接定位a memory unit;
  3. [bxtidata]Use a variable and constant to represent the address, and you can 起始地址的基础use a variable and 间接定位a memory unit on one;
  4. [bx+si]Use two variables to represent the address;
  5. [bx+sitidata]Addresses are represented by two variables and a constant.

It can be seen that from [idata]all the way to [bx+sitidata], it is to use a more flexible way to locate the address of a memory unit. This allows us to look at the data we are dealing with from a more structured perspective.
The essence is to first []calculate the result 寄存器of 常量the neutralization expression as a physical address, and then point to that piece of memory.

Question 7.6

Program to change the first letter of each word in the datasg segment to uppercase
insert image description here

assume cs:codesg,ds:datasg
datasg segment				;16*6=16字节
	db	'1. file         '	;16字节,末尾空格填充
	db	'2. edit         '  ;16字节,末尾空格填充
	db	'3. search       '  ;16字节,末尾空格填充
	db	'4. view         '  ;16字节,末尾空格填充
	db	'5. options      '  ;16字节,末尾空格填充
	db	'6. help         '  ;16字节,末尾空格填充
datasg ends
codesg segment
 start:	mov ax, datasg
		mov ds, ax
		
		mov bx, 0
		mov cx, 6
 	s:	mov al,[bx+3]		; 取 bx行第3列
		and al, 11011111b	; 转大写
		mov [bx+3], al		; 送回内存
		add bx, 16
		loop s
		
		mov ax, 4c00h		
		int 21h
codesg ends
end start

insert image description here

Question 7.7

Programming, change each word in the datasg section to capital letters
insert image description here
This section is a negative teaching material, two loops share CX and fall into an endless loop.

assume cs:codesg,ds:datasg
datasg segment				;16*4=24字节
	db	'ibm             '	;16字节,末尾空格填充
	db	'dec             '  ;16字节,末尾空格填充
	db	'dos             '  ;16字节,末尾空格填充
	db	'vax             '  ;16字节,末尾空格填充

datasg ends
codesg segment
 start:	mov ax, datasg
		mov ds, ax
		
		mov bx, 0
		mov cx, 4			; 循环4次对应4行
	s0: mov si, 0			; 每行的字符索引从0开始
		mov cx, 3			; 本意是循环3次对应3个字符
							; 但是它覆盖了上面“行”的循环计数。
							; 两层循环共用同一个cx计数,就崩了。
		
 	s:	mov al,[bx+si]		; 取 bx行第3列
		and al, 11011111b	; 转大写
		mov [bx+si], al		; 送回内存
		inc si				; 内层循环j++ 指向下一个字符
		loop s				; 内层循环
		
		add bx, 16			; 外层循环i++ 指向下一歫
		loop s0				; 外层循环
		
		mov ax, 4c00h		
		int 21h
codesg ends
end start

Question 7.8

Fix bugs in 7.7. Every time the inner loop starts, save the value in cx of the outer loop, and restore the value of cx in the outer loop before executing the loop instruction of the outer loop. Register dx can be used to temporarily save the value in cx, and the improved procedure is as follows.

assume cs:codesg,ds:datasg
datasg segment				;16*4=24字节
	db	'ibm             '	;16字节,末尾空格填充
	db	'dec             '  ;16字节,末尾空格填充
	db	'dos             '  ;16字节,末尾空格填充
	db	'vax             '  ;16字节,末尾空格填充

datasg ends
codesg segment
 start:	mov ax, datasg
		mov ds, ax
		
		mov bx, 0
		mov cx, 4			; 循环4次对应4行
	s0: mov dx, cx			; 将外层循环的 cx 值保存在 dx 中
		mov si, 0			; 每行的字符索引从0开始
		mov cx, 3			; 循环3次对应3个字符
		
 	s:	mov al,[bx+si]		; 取 bx行第3列
		and al, 11011111b	; 转大写
		mov [bx+si], al		; 送回内存
		inc si				; 内层循环j++ 指向下一个字符
		loop s				; 内层循环
		
		add bx, 16			; 外层循环i++ 指向下一行
		mov cx, dx			; 用 dx 中存放的外层循环的计数值恢复 cx
		loop s0				; 外层循环
		
		mov ax, 4c00h		
		int 21h
codesg ends
end start

insert image description here

Question 7.9

Programmatically, change the first 4 letters of each word in the datasg segment to capital letters

assume cs:codesg,ss:stacksg,ds:datasg

stacksg segment				;16字节
	dw 0,0,0,0,0,0,0,0
stacksg ends

datasg segment				;64字节
	db '1. display      '	;16字节
	db '2. brows        '	;16字节
	db '3. replace      '   ;16字节
	db '4. modify       '   ;16字节
datasg ends

codesg segment	
start:
codesg ends                   
end start

insert image description here

Lab 6 Procedures in the Practical Course

"Assembly Language" - Reading Notes - Program in Experiment 6 Practical Course

Guess you like

Origin blog.csdn.net/jx520/article/details/130885094