Calculator assembly language based on 51 single chip microcomputer

Publisher:快乐飞翔Latest update time:2022-03-06 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The calculators we talked about before were all based on C language. Next, let’s talk about a simple calculator based on assembly language.


hardware design


The display is still a digital tube, and the MCU is C51, which supports integer calculation.

Simulation diagram:

insert image description here

programming

YJ EQU 50H ;Result storage

YJ1 EQU 51H ;Intermediate result storage

GONG EQU 52H ; Function key storage

          ORG 0000H

START: MOV R3,#0 ; Initialization display is empty

          MOV GONG,#0      

          MOV 32H,#00H

          MOV 33H,#00H

          MOV 34H,#00H

MLOOP: CALL DISP ;Call display subroutine

WAIT: CALL TESTKEY ; Check if a key is pressed

          JZ WAIT ; Check if the accumulator is 0 

          CALL GETKEY ; read key

          INC R3 ;Number of buttons

          CJNE A,#0,NEXT1 ; Determine whether it is a numeric key

          LJMP E1 ; Switch to numeric key processing

NEXT1: CJNE A,#1,NEXT2

          LJMP E1

NEXT2: CJNE A,#2,NEXT3

          LJMP E1

NEXT3: CJNE A,#3,NEXT4

          LJMP E1

NEXT4: CJNE A,#4,NEXT5

          LJMP E1

NEXT5: CJNE A,#5,NEXT6

          LJMP E1

NEXT6: CJNE A,#6,NEXT7

          LJMP E1

NEXT7: CJNE A,#7,NEXT8

          LJMP E1

NEXT8: CJNE A,#8,NEXT9

          LJMP E1

NEXT9: CJNE A,#9,NEXT10

          LJMP E1

NEXT10: CJNE A,#10,NEXT11; Determine whether it is a function key

          LJMP E2 ; Switch to function key processing

NEXT11: CJNE A,#11,NEXT12

          LJMP E2

NEXT12: CJNE A,#12, NEXT13

          LJMP E2

NEXT13: CJNE A,#13,NEXT14

          LJMP E2

NEXT14: CJNE A,#14,NEXT15

          LJMP E2

NEXT15: LJMP E3; Determine whether to clear the key

E1: CJNE R3,#1,N1; Determine which key is pressed

          LJMP E11 ; for the first number

N1: CJNE R3,#2,N2

          LJMP E12; for the second number

N2: CJNE R3,#3,N3

          LJMP E13 ; for the third digit

N3: LJMP E3; The fourth digit overflows

E11: MOV R4,A; Input value is temporarily stored in R4

          MOV 34H,A ; input value is sent to display buffer

          MOV 33H,#00H

          MOV 32H,#00H

          LJMP MLOOP ; Wait for input again

E12: MOV R7,A; single digit is temporarily stored in R7

          MOV B,#10

          MOV A,R4

          MUL AB ; Tens digit

          ADD A,R7

          MOV R4,A ; input value stored in R4

          MOV 32H,#00H ; Input value is sent to display buffer

          MOV 33H,34H

          MOV 34H,R7

          LJMP MLOOP ; Wait for input again

E13: MOV R7,A

          MOV B,#10

          MOV A,R4

          MUL AB

          JB OV,E3; Input overflow, if OV is equal to 1, go to E3

          ADD A,R7

          JB CY,E3 ; Input overflow

          MOV R4,A

          MOV 32H,33H ; input value to display buffer

          MOV 33H,34H

          MOV 34H,R7

          LJMP MLOOP

E3: MOV R3,#0 ; reset the number of key presses

          MOV R4,#0 ; input value cleared

          MOV YJ,#0 ; Clear the calculation result

          MOV GONG,#0 ; Set the function key to zero      

          MOV 32H,#00H ; Display clear

          MOV 33H,#00H

          MOV 34H,#00H

          LJMP MLOOP

E2: MOV 34H,#00H

           MOV 33H,#00H

           MOV 32H,#00H

           MOV R0,GONG ;Exchange with the last function key

           MOV GONG,A

           MOV A,R0

           CJNE A,#10,N21 ;Judgement function key

           LJMP JIA ;"+"

N21: CJNE A,#11,N22

           LJMP JIAN ;"-"

N22: CJNE A,#12,N23

           LJMP CHENG ;"*"

N23: CJNE A,#13,N24

           LJMP CHU ;"/"

N24: CJNE A,#0,N25

           LJMP FIRST ; Press the function key for the first time

N25: LJMP DEN ;"="

N4: LJMP E3

FIRST: MOV YJ,R4; input value to result

           MOV R3,#0 ; reset the number of key presses

           LJMP DISP1 ; Result processing

JIA: MOV A,YJ ; send the last result to the accumulator

           ADD A,R4; last result plus input value

           JB CY,N4 ;Overflow

           MOV YJ,A ; store this result

           MOV R3,#0 ; reset the number of key presses

           LJMP DISP1

JIAN: MOV A,YJ

           SUBB A, R4 ; Last result minus input value

           JB CY,N4 ; Negative overflow

           MOV YJ,A

           MOV R3,#0

           LJMP DISP1

CHENG: MOV A,YJ

           MOV B,A

           MOV A,R4

           MUL AB ; last result multiplied by input value

           JB OV,N4 ;Overflow

           MOV YJ,A                 

           LJMP DISP1

CHU: MOV A,R4

           MOV B,A

           MOV A,YJ

           DIV AB ; divide the last result by the input value

           MOV YJ,A

           MOV R3,#0

           LJMP DISP1

DEN: MOV R3,#0

           LJMP DISP1

DISP1: MOV B,#10

           MOV A,YJ ; send the result to the accumulator

           DIV AB ; Divide the result by 10

           MOV YJ1,A ; Temporarily store "商"

           MOV A,B ; take the unit digit

           MOV 34H,A ; Send the single digit to the display buffer

           MOV A,YJ1

           JZ DISP11 ; Is the result a single digit?

           MOV B,#10

           MOV A,YJ1

           DIV AB

           MOV YJ1,A

           MOV A,B

           MOV 33H,A ; Send the ten digits to the display buffer

           MOV A,YJ1

           JZ DISP11 ; Is the result a two-digit number?

           MOV 32H,A ; send the hundreds digit to the display buffer

DISP11: LJMP MLOOP


The file is for reference only:


Link: https://pan.baidu.com/s/1ZAxAbTe_oD_cuTvvKWxU5A

Extraction code: x102

Reference address:Calculator assembly language based on 51 single chip microcomputer

Previous article:Simple lottery machine based on 51 single chip microcomputer
Next article:A multifunctional calculator based on 51 single-chip microcomputer, supporting binary and octet decimal negative number calculation

Latest Microcontroller Articles
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号