A brief discussion on the conversion between C language and assembly language in single chip microcomputer

Publisher:等放假的LwjLatest update time:2017-08-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I have designed a single-chip microcomputer, which needs to be implemented using both C language and assembly language. Now I write down my feelings and gains from this design, as well as the problems I encountered. Friends who are interested are welcome to exchange ideas and make suggestions.

Single chip microcomputer design: 99 code table design based on 51 single chip microcomputer

Software environment: Proteus8.0 + Keil4

Requirements: 1. Press the switch once, and the digital tube starts timing. 2. Press it twice, and the digital tube display is still. 3. Press it three times, and the digital tube value is reset to zero.

The C language program is as follows:

#include

#define uint unsigned int

#define uchar unsigned char

uchar shi,ge,aa,keycount=0,temp;

sbit anjian=P1^7;

uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

void display(shi,ge);

void key ();

void init();

void delay(uint z);

/*-----Main program-----*/

void main()

{

init(); //initialization

while(1)

{

key();

if(keycount==1)

TR0=1; //Open interrupt

if(keycount==2)

TR0=0;

if(keycount==3)

{

temp=0;

keycount=0;

}

if(aa==10){aa=0;

if(temp<=99)

{

temp++;display(shi,ge);

}

else

temp=0;}

}

}

/*------Initialization procedure-------*/

void init()

{

keycount=0;

temp=0;

TMOD=0x01;

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

EA=1;

ET0=1;

//TR0=0;

}

/*-----Timer Interrupt-----*/

void timer0() interrupt 1

{

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

aa++;

}

/*-----Display subroutine-----*/

void display(shi,ge)

{

shi=temp/10;

ge=temp%10;

P0=table[shi];;delay(70);

P2=table[ge]; ;delay(70);

}

/*-----Key detection subroutine-----*/

void key ()

{

if(anjian==0)

{

delay(5); //debounce

if(anjian==0)

keycount++;

}

//while(anjian==0);

//display(shi,ge); //Wait for the button to pop up

}

/*-----Delay subroutine-----*/

void delay(uint z) //delay about 1ms

{

uint x,y;

for(x=z;x>0;x--)

for(y=100;y>0;y--);

}

The circuit simulation results are as follows:

Okay, then let's start the journey from C language to assembly language^_^

(1) Change lines 1-10 of the C language to

ORG 0000H //Assembly start pseudo instruction, which specifies the starting address of the source program or data block in the program memory

ajmp STAR //ajmp unconditional jump instruction

ORG 000bh

ajmp timer0

anjian equ P1.7 //bit definition

keycount equal 40h

shi equ 41h

42h

aa equ 43h

Temp 44h

tab: db 3fh,6h,5bh,4fh,66h //Create table

db 6dh,7dh,7h,7fh,6fh

(2) Change lines 12-14 and 39-49 of the initialization function in C language to

1 STAR: 2 acall init //Subroutine short-range call instruction, the function is that the main program calls the subroutine, the range of calling the subroutine is 2kb

init:

mov keycount,#0 //keycount=0

mov temp,#0 //temp=1

mov tmod,#01h //TMOD=0x01

mov TH0,#60

mov TL0,#176

setb EA //position instruction, set the bit indicated by the operand to 1

setb ET0

setb TR0

ret

acall is a subroutine short-range call instruction, and ret is used to return.

(3) Lines 15-35 in C language are a while loop with complicated logic. Pay attention!

START:

acall display

inc temp //add 1 instruction, add 1 to the content of the unit or register specified by the operand

acall delay70 // short-range call delay70

x8: mov r0,keycount

cjne r0,#2,F1 //cjne comparison jump instruction, if r0=2, jump to x8, otherwise jump to F1.

ajmp x8

F1: mov r0,temp

cjne r0,#99,START //If r0<99, repeat the loop, otherwise temp=0

mov temp,#0

ajmp START

F9:

acall key

mov r0,keycount

cjne r0,#0,F2 //keycount=0 execute sequentially, otherwise jump to F1

CLR P1.3 //Clear

SETB TR0


F2: mov r0,keycount //Second key press

cjne r0,#2,F2

clr TR0

reti

mov r0,keycount // third key press

cjne r0,#3,F3

mov temp,#0

mov keycount,#0

The inc increment instruction adds 1 to the content of the unit or register specified by the operand, and returns the result back to the original operand unit.

The clr bit is reset, and its function is to clear the bit indicated by the operand to "0".

Or in the interrupt function

timer0:

w1:

acall key

mov TH0,#60

mov TL0,#176

cpl p1.0

JB keycount,x2

ajmp x3

x2:

ajmp START

clr p1.0

ajmp w1

ajmp w1

x3: mov r0,keycount

cjne r0,#3,w1 //If r0=3, execute sequentially, otherwise jump to w1

mov temp,#0

mov keycount,#0

ret

(4) Change the display function in lines 58-64 of the C language to

display:

mov a,temp

mov b,#10

div ab //Division instruction, implements the division operation of two eight-bit unsigned numbers.

mov r2,A

mov r3,B

mov dptr,#tab //16-bit data transfer usage

mov a,r2

movc a,@a+dptr //Look up the table, first add the contents of accumulator A to the contents of data pointer register DPTR, then use the result as the address and send the result of the address to A

mov P0,a

acall delay70

nop //empty instruction

mov a,r3

movc a,@a+dptr

mov P2,a

nop

acall delay70

ret

Div is a division instruction, which is used to implement the division operation of two 8-bit unsigned numbers. Generally, the dividend is placed in accumulator A and the divisor is placed in register B. After the instruction is executed, the quotient is placed in A and the remainder is placed in B.

movc is a table lookup instruction. It first adds the contents of the accumulator A to the contents of the data pointer register DPTR, and then uses the result as the address to send the contents of the address into A.

nop is a no-operation instruction. It does not perform any operation, but takes up one machine cycle (i.e. 12 oscillation cycles) and is often used for delay or waiting. (The effect of some program execution cannot be clearly recognized by the human eye due to the short delay time)

The purpose of this program is to display a two-digit number on a digital tube at the tens place and a digital tube at the ones place.

(5) The key function in lines 66-76 of the C language is changed to

1 key:2 jb anjian,F6 //If anjian=0, execute sequentially, otherwise jump to F63 ACALL delay54 inc keycount //keycount++5 F6: 6 ret

jb is a bit-conditional transfer instruction. Its function is that if the directly addressed bit = 1, the program will transfer to the specified target address for execution. If the bit = 0, the program will be executed sequentially.

(6) Change the delay function in lines 78-83 of the C language to

delay70:

mov r6,#70

D2: mov R7,#248

d1: djnz R7,d1 //248*70 times

DJnz R6,D2

ret


delay5:

mov r6,#5 // eliminate jitter.

F7: mov R7,#248

F8: djnz r7,F8 //248*5 times

djnz r6,F7

ret

Note: 248 = 2 8 , which is approximately equal to 1ms. delay is the delay program.

Warm reminder: The case of the program code is not affected in assembly language, but it is affected in C language.

Question 1: Both ret and reti are program return instructions, what is the difference?

My answer: ret is the subroutine return instruction, and reti is the interrupt subroutine return instruction. The difference is that if the subroutine is called by the acall or lcall instruction, the return instruction is ret; if the subroutine is called by the address 0003, 0013, 000B, 001B, or 0023, the return instruction is reti.

Question 2: mov 20h, #0h and setb 20h all add 1, what is the difference?

My answer: 20h in mov instruction refers to byte, 20h in setb refers to bit.

The journey ends!

I still remember that I was struggling with the syntax and functions of various instructions in assembly language some time ago. Until one sunny afternoon, I took the two and a half pages of C language code that I had written in one hand and a single-chip assembly instruction query manual in the other hand, and began to translate line by line. The assembly code may have some errors in debugging, but the basic logic is correct. Moreover, this time C -> assembly, I have a deeper understanding of the storage and call of data in the computer. During this period, the head teacher and classmates also answered my questions. I believe that in the future, I will have a deeper understanding of computers. The harder you work, the luckier you are!


Reference address:A brief discussion on the conversion between C language and assembly language in single chip microcomputer

Previous article:51 single chip microcomputer stepper motor control, forward and reverse rotation, etc.
Next article:STC12C5A60S2 MCU realizes ISP automatic download

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号