Assembly-subprogram design based on MCS-51 kernel

Publisher:cw57324588Latest update time:2021-11-30 Source: eefocusKeywords:MCS-51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

What is a subroutine?

In the actual programming process, in order to reduce the amount of program code, some frequently used instruction sets are called subroutines; this can be compared to the delay function delay() in C language, etc.


Function:

In order to solve the troublesome operation of repeatedly using the same set of program codes, just call it every time you need it;

Subroutine execution characteristics:

1. Called by other programs;

2. After execution, the execution flow needs to be returned to the main program of the subroutine;


~A gorgeous dividing line~~~


The above content is too simple. I believe that comrades who have learned a little bit of other languages ​​can deeply understand it. Here we mainly focus on the explanation of assembly language; to explain the details that we often use high-level languages ​​but habitually ignore (details are also functions that are directly optimized by high-level languages ​​and are not seen)


Here comes the point!

Two points to note when calling a subroutine:

1. Protection and restoration of the site;

2. Transfer of parameters between main program and subprogram


Protection and restoration of the site

Let’s imagine a scenario:

Before calling a subroutine, a general unit (accumulator A) of the microcontroller stores a valuable data that will be used after the subroutine is executed. However, since this general unit is used when the subroutine is executed, the value is no longer found after exiting the subroutine.

Well, given this situation, we have the argument for protection and restoration;

Common units include: R0~R7, accumulator A, data pointer DPTR, flags and status, etc.;


Parameter passing


Passing parameters using accumulators

This method can directly "play the trick on the opponent". Anyway, the value is already in accumulator A, so it can be used directly in the subroutine. When the subroutine returns a value, it can also directly return the value to accumulator A.


For example:


/*Assume that the memory addresses 30H, 31H, 32H, and 33H are used to store the values ​​of a, b, c, and d respectively. Now we need to divide d by a square + b square + c square */

ORG 0000H ; Set the program start storage address

START: MOV A,30H; Take the data at address 30H and send it to accumulator A

ACALL SQR; Call to check the square table (the square table is the array defined by the DB)

MOV R1,A; a square is temporarily placed in R1

MOV A,31H; take the value of b at address 30H and send it to accumulator A

ACALL SQR ;Call subroutine `Write code snippet here`

ADD A,R1 ;A=a方+b方

MOV R1,A ;R1 = A

MOV A,32H

ACALL SQR

ADDC A, R1

MOV 33H,A

SJMP $

;Subroutines

SQR: MOV DPTR,#TAB; assign the address of the first element of the square table to the DPTR data pointer

MOVC A,@A+DPTR; assign the corresponding data value in the TAB square table to accumulator A

RET; Subroutine return instruction. After execution, the two contents at the top of the stack are popped out and sent to the PC, and SP (stack top pointer) is reduced by 2; then the 16-bit address is placed in the PC;

TAB: DB 0,1,4,9,6,25,36,49,64,81; DB is a byte definition instruction, and each subsequent data occupies one byte;

END


Passing parameters using the stack

Here is a stack concept:

What is a stack: a storage area; a storage area opened up in the RAM;

Stack function: specially used to temporarily store data or return address;


What are the stack transfer parameters:

Using the stack to pass parameters is a method often used in subroutine nesting;

How to pass parameters:

Before calling a subroutine, use the PUSH instruction to push the data required by the subroutine into the stack.

When executing the subroutine, use the POP instruction to pop data from the stack;


For example:


/*question:

Write a subroutine for 0! +1! +2! +3! +4!

Unit 20H stores the number to be factored, and unit 30H stores the factorial calculated each time the subroutine is called.

The problem is on lines 30 and 31 of the program.

*/

ORG 0000H


MOV SP,#60H; Initialize SP=60H; Note that 60H here is an address instead of an integer;

MOV 20H,#0H; store the number to be factorialized;

MOV R2,#04H ;Number of cycles

MOV A,#0H


START: PUSH 20H; SP=SP+1, the integer 20H is given to the stack register at address 61H

PUSH ACC; SP = SP + 1, here the integer value stored in ACC is assigned to the register unit at address 62H, which should not be A but ACC

ACALL MU; call the subroutine at MU, SP=SP+2, and send the address of PC here to SP

POP ACC

POP 30H

ADD A,30H

INC 20H

DJNZ R2,START  

SJMP $


MU: MOV R1,SP ; borrow R1 as stack pointer

DEC R1 ;Protect the scene when programming

DEC R1 ;Protect the scene when programming

DEC R1 

DEC R1; R1 points to the number to be factorialized

MOV A,@R1; Take out the number to be factorial

ADD A,#02H ; ? Why does A need to be incremented by 2? Is it related to the program in line 31?

MOVC A,@A+PC; Lookup table? How does CP change from the top-down execution state to the table lookup state?

XCH A,@R1 ; Whole byte exchange

RET ; Return to the address of the main program

TAB: DB 0,1,2,6,24,120

END

Additional knowledge:

There was a problem in the above program code:

PUSH A

POP  A

Error: Expression does not match

PUSH ACC

POP ACC

No error is reported;

wrong reason:

Although A and ACC are the same thing; but...PUSH and POP instructions must be followed by a direct address, but A is compiled into accumulator A (not address). When the compiler compiles, A is considered a register and ACC is considered a special function register (address); so an error is reported;

Keywords:MCS-51 Reference address:Assembly-subprogram design based on MCS-51 kernel

Previous article:Teach you how to learn 51 single chip microcomputer: learn the basic knowledge of hardware
Next article:Notes on assembly operators based on the MCS-51 kernel

Recommended ReadingLatest update time:2024-11-16 13:48

What are the differences in the P0~P3 port structures of the MCS-51 microcontroller?
What are the differences in the structure of the P0 to P3 ports of the MCS-51 microcontroller? What should be paid attention to when using them as general I/O ports to input data? Answer: P2 port is a dual-function port. It is a general I/O port and a high 8-bit address port when accessing external memory in bus mod
[Microcontroller]
Realizing fireworks function with dot matrix screen based on MCS-51 single chip microcomputer
Do you remember the wonderful moment when the fireworks burst? When I was a child, whenever I watched the fireworks burst, I always hoped that I could keep the most beautiful moment forever. Finally, today my dream came true. With my own board, I can do it with just a few programmings! Before showing you the ef
[Microcontroller]
Realizing fireworks function with dot matrix screen based on MCS-51 single chip microcomputer
MCS-51 MCU scanning matrix keyboard program
#include reg52.h #define uint unsigned int #define uchar unsigned char sbit DSCQ=P2^6; //bit defines the segment latch control terminal sbit WSCQ=P2^7; //bit definition bit latch control terminal uchar code table ={0x3f,0x06,0x5b,0x4f, //0,1,2,3 0x66,0x6d,0x7d,0x07, //4,5,6,7 0x7f,0x6f,0x77,0x7c, //8,9,a,
[Microcontroller]
OK6410A development board (eight) 16 linux-5.11 OK6410A start_kernel print angle second stage do_initcalls
log // init_jiffies_clocksource- ... - __clocksource_register_scale clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns // futex_init futex hash table entries: 256 (order: -1, 3072 bytes, linear) // netlink_proto_init NET: Registered protocol family 16 // atomic_pool_
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号