Introduction to compilation (1)

Introduction to compilation (1)

1. The first assembler

Currently using keil software, the extension must be asm when creating the file.
Insert picture description here
Insert picture description here
How to view the value
in the specific address memory: Select "VIEW"> "memory windows"> "memory1" on the toolbar
and enter the address:
Insert picture description here
Insert picture description here
pay attention here The first column of the first row is 0x1B, then the next one on the right is 0x1C

The first assembler:

ORG 0000H        ;伪指令,定义下面的指令所在地址,此句为主程序的开始地址
LJMP MAIN        ;无条件转移到MAIN
MAIN:            ;主程序
	MOV 30H,#40H ;30H的内容为40H
	MOV 40H,#10H ;40H的内容为10H
	MOV R0,#30H  ;R0的内容为30H
	MOV A,@R0    ;A的内容为40H
	MOV R1,A
	MOV B,@R1
	SJMP $       ;转到该指令的开头开始执行

END              ;程序结束

Assembler exercise:
1. The compressed BCD code in A is divided into two bytes to form a non-compressed BCD code, which is put into 40H and 41H units

	MOV R0,A
	ANL A,#00001111B   ;ANL:与运算
	MOV 40H,A
	MOV A,R0     ;取回原数据
	ANL A,#11110000B
	SWAP A       ;将A中高四位与低四位交换
	MOV 41H,A

2. There are two 4-bit BCD codes, which are respectively stored in the 50H~51H unit and 60H~61H unit of the internal data memory
; try to write a program to calculate the sum of these two numbers, and the result will be stored in the 40H~41H unit

MOV A,50H	  ;A←(50H)
ADD A,60H	  ;低两位相加,A←(A)+(60H) 
DA A		  ;进行 BCD 码修正 ,DA A 指令为十进制调整指令
MOV 40H,A	  ;将修正后的低两位结果送 40H 
MOV A,51H	  ;A←(51H) 
ADDC A,61H	  ;高两位带上低位的进位位相加,A←(A)+(61H)+CY 
DA A		  ;进行 BCD 码修正 
MOV 41H,A	  ;将修正后的高两位结果送 41H

3.; The contents of units 20H~3FH in the internal data RAM are transferred to, and the external data is stored in continuous units starting with 2000H.
There are 32 units in total from 20H to 3FH, and data needs to be transmitted 32 times. R1 acts as a loop counter.

MOV  R0,#20H       ;设置R0为内部RAM首地址 
MOV  DPTR,#2000H   ;设置外部RAM首地址 
MOV  R1,#32        ;设R1为计数器 
LOOP:
	MOV A,@R0          ;取内部RAM数 
	MOVX @DPTR,A       ;送外部 RAM 
	INC  R0            ;调整内部RAM指针,指向下一个数据 
	INC  DPTR          ;调整外部RAM指针 
	DJNZ R1,LOOP       ;未完继续    
SJMP  $                ; 停机 

4. Change the upper 4 bits of the data in the off-chip RAM space 2000H~200AH to zero, the lower 4 bits remain unchanged, and the original address is stored

	MOV DPTR,#2000H     ;数据指针DPTR存开始地址
	MOV R1,#0BH         ;B=11,一共是0到A:12个数据
	LOOP: MOVX A,@ DPTR ;循环指令
	      ANL A,#0FH    ;与运算,高四位清0,低四位不变
		  MOVX @ DPTR,A ;题目要求原址存放
		  INC DPTR      ;DPTR加一
		  DJNZ R1,LOOP  ;R1减一,若此时R1不为0,则跳转至LOOP
	SJMP $	 

5. The 10 unsigned numbers from 40H to 49H in the internal data RAM are compared one by one, and are arranged in this unit in order from small to large.

START:  CLR F0         ;清除交换标志位F0 
		MOV  R3,#9     ;十个数据循环次数         
		MOV  R0,#40H   ; R0存放区首址         
		MOV  A,@R0     ; 取前数 
		
L2:     INC  R0        ;R0加1 
		MOV R2,A       ;保存前数         
		SUBB A,@R0     ;前数减后数,如果是小数减大数,进位C会被置1        
		MOV A,R2       ;恢复前数         
		JC L1          ;顺序则继续比较.如果进位为1则转到L1        
		SETB F0        ;逆序则建立标志位         
		XCH A,@R0      ;前数与后数交换         
		DEC R0         ;R0减1,指向前数单元         
		XCH A,@R0         
		INC R0         ;仍指向后数单元 
		
L1:     MOV A,@R0      ;取下一个数         
		DJNZ R3,L2     ;依次重复比较,R3减1,不为0 则转移到L2         
		JB F0,START    ;交换后重新比较,如果F0为1 则转移到START         
		RET            ;从子程序返回
		END


Guess you like

Origin blog.csdn.net/weixin_44026026/article/details/109232485