当前位置:网站首页>Assembly (receive several n-digit decimal values (0~65535) from the keyboard and display their sum in different base numbers.)
Assembly (receive several n-digit decimal values (0~65535) from the keyboard and display their sum in different base numbers.)
2022-06-23 08:38:00 【User 9372190】
@toc
subject
Receive several... From the keyboard N Decimal number of digits (0~65535), And in binary 、 Decimal system 、 Hexadecimal three number system forms show its sum .
requirement :
(1) Using subroutines to achieve a N Input of decimal value , In the loop structure of the main program
Call the subroutine in ;
(2) When the user does not enter a value , When entering directly , End input ;
(3) The output data is multi digit decimal data , The sum calculated inside the machine is in hexadecimal form , Number system conversion is required , Then output the result as a decimal string ;
(4) Necessary prompt information is required in the program .
Run the example
Code
DATA SEGMENT
STR1 DB "Please input a number: $"
STR2 DB "The sum is: $"
CRLF DB 0AH,0DH,'$' ; Line break
COUNT DW 0 ; Save all input true and
DIVNUM DW 10
DIVNUM1 DW 16
MULNUM DW 10
RESULT DB 5 DUP(?)
RESULT1 DB 5 DUP(?)
KONGGE DB 32,32,32,32,32,32,32,32,32,32,32,32,'$' ; Output multiple spaces , Use... When aligning
TEMP DW ?
ARRAY DB '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' ; When used to output hexadecimal form , Compare with subscript
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
LOOP1:
CALL GET
CMP CX,0
JZ ENDINPUT
ADD COUNT,BX
JMP LOOP1
ENDINPUT:
LEA DX,CRLF
MOV AH,9
INT 21H
LEA DX,STR2
MOV AH,9
INT 21H
TWO:
MOV BX,COUNT
MOV TEMP,0
LOOPTWO:
CMP TEMP,16
JZ TEN
SAL BX,1
JNC OUT0
MOV AH,02H
MOV DL,'1'
INT 21H
ADD TEMP,1
JMP LOOPTWO
OUT0:
MOV AH,02H
MOV DL,'0'
INT 21H
ADD TEMP,1
JMP LOOPTWO
TEN:
MOV AH,02H
MOV DL,'B'
INT 21H
LEA DX,CRLF
MOV AH,9
INT 21H
LEA DX,KONGGE
MOV AH,9
INT 21H
MOV AX,COUNT
MOV SI,0
MOV DX,0
LOOP3:
DIV DIVNUM; AX...DX
MOV RESULT[SI],DL ; Because the remainder is less than 10, therefore DH=0
CMP AX,0
JZ NEXT
MOV DX,0
ADD SI,1
JMP LOOP3
NEXT:
ADD SI,1
MOV CX,SI
SUB SI,1
LOOP4:
ADD RESULT[SI],30H
MOV AH,02H
MOV DL,RESULT[SI]
INT 21H
SUB SI,1
LOOP LOOP4
MOV AH,02H
MOV DL,'D'
INT 21H
LEA DX,CRLF
MOV AH,9
INT 21H
LEA DX,KONGGE
MOV AH,9
INT 21H
SIXTEEN:
MOV AX,COUNT
MOV CX,4
MOV SI,0
MOV DX,0
LOOPM:
DIV DIVNUM1; AX....DX
MOV RESULT1[SI],DL ; Although the remainder is placed in DX in , But the remainder will not exceed fifteen , therefore DH by 0
CMP AX,0
JZ T
MOV DX,0
ADD SI,1
LOOP LOOPM
T:
ADD SI,1
MOV CX,SI
SUB SI,1
LOOP5:
MOV BL,RESULT1[SI]
MOV BH,0
MOV DI,BX
MOV AH,02H
MOV DL,ARRAY[DI]
INT 21H
SUB SI,1
LOOP LOOP5
MOV AH,02H
MOV DL,'H'
INT 21H
LEA DX,CRLF
MOV AH,9
INT 21H
MOV AX,4C00H
INT 21H
GET PROC ; No input value , There is no output , But store the results in BX in
LEA DX,STR1
MOV AH,9
INT 21H
MOV CX,0;CX Used to count , It is used to judge whether this input is only the input of "enter"
MOV BX,0; use BX Save the last true value of the entered number
LOOPT:
MOV AH,1
INT 21H
CMP AL,0DH
JZ OVER
MOV CX,1
MOV AH,0
SUB AL,30H
ADD AX,BX
MUL MULNUM;DW The type and DW Type multiplication , The top 16 places are DX Medium , The 16th place is AX
MOV BX,AX
JMP LOOPT
OVER:
MOV AX,BX
DIV DIVNUM
MOV BX,AX
RET
GET ENDP
CODE ENDS
END STARTexplain
1、 Input part
LOOP1:
CALL GET
CMP CX,0
JZ ENDINPUT
ADD COUNT,BX ; Will each time the real NUM Add to COUNT in
JMP LOOP1
ENDINPUT:
LEA DX,CRLF; Line break
MOV AH,9
INT 21H
LEA DX,STR2; Output The sum is:
MOV AH,9
INT 21HGET PROC ; No input value , There is no output , But store the results in BX in
LEA DX,STR1
MOV AH,9
INT 21H
MOV CX,0;CX Used to count , It is used to judge whether this input is only the input of "enter"
MOV BX,0; use BX Save the last true value of the entered number
LOOPT:
MOV AH,1
INT 21H
CMP AL,0DH
JZ OVER
MOV CX,1
MOV AH,0
SUB AL,30H
ADD AX,BX
MUL MULNUM;DW The type and DW Type multiplication , The top 16 places are DX Medium , The 16th place is AX
MOV BX,AX
JMP LOOPT
OVER:
MOV AX,BX
DIV DIVNUM
MOV BX,AX
RET
GET ENDPHolistic thinking
The difference between this question and the previous one is that the input is not a single decimal system (0~9) Number of numbers
Here I use a character by character to receive user input , Such as input 6552, Receive the first character 6, hold 6 ride 10, Put it in a register BX in ; Then receive 5, take BX+5, So now BX The value in is 65 了 , then BX ride 10( The multiplication here 10 And the subsequent multiplication with the help of AX); Then receive 5, take BX+5, So now BX The value in is 655 了 , then BX ride 10; Then receive 2, take BX+2, Now? BX by 6552, take BX ride 10,
Because I am.
MOV AX,BX MUL MULNUM MOV BX,AX
( If you take 10 No more than 65535 Words , It doesn't work DX), Now? DX:AX The value in is 65520 . Now it has been received , Enter by user , Exit the cycle of input characters , take DX:AX except 10 that will do
Step by step
LOOP1:
CALL GET
CMP CX,0
JZ ENDINPUT
ADD COUNT,BX
JMP LOOP1First a loop CALL GET
have a look GET Subroutines
LEA DX,STR1
MOV AH,9
INT 21H
MOV CX,0;CX Used to count , It is used to judge whether this input is only the input of "enter"
MOV BX,0; use BX Save the last true value of the entered number Output STR1, Here I use CX To judge , Because the user only enters a carriage return, it means that the user ends the whole input part , If user input 6553 Enter enter again , Represents just the end of this input , Will continue to enter the next number . So here I use CX Judge whether other numbers have been entered before entering
LOOPT:
MOV AH,1 ; User input characters
INT 21H
CMP AL,0DH ; Judge whether to enter , If it is enter, exit the cycle , This is the only condition for exiting the loop
JZ OVER
MOV CX,1 ; It's not a return , Prove that there is now a digital input , therefore CX by 1, The next time the user will enter a complete NUM
MOV AH,0
SUB AL,30H ;AX Save the real value of the input character
ADD AX,BX ;
MUL MULNUM;DW The type and DW Type multiplication , The top 16 places are DX Medium , The 16th place is AX
MOV BX,AX
JMP LOOPT
OVER:
MOV AX,BX
DIV DIVNUM
MOV BX,AXRead notes , And the whole thought just now has made it clear
2、 Output hex
Take the output hexadecimal as an example
Actually, the output part is quite simple
SIXTEEN:
MOV AX,COUNT; Assign and to AX
MOV CX,4; Give Way CX by 4 As the number of cycles , the reason being that 16 Bit , Convert it to hexadecimal 4 position
MOV SI,0 ; Used as index register , As a pointer
MOV DX,0
LOOPM:
DIV DIVNUM1; AX....DX
MOV RESULT1[SI],DL ; Although the remainder is placed in DX in , But the remainder will not exceed fifteen , therefore DH by 0
CMP AX,0
JZ T
MOV DX,0
ADD SI,1
LOOP LOOPM
T:
ADD SI,1
MOV CX,SI
SUB SI,1
LOOP5:
MOV BL,RESULT1[SI]
MOV BH,0
MOV DI,BX
MOV AH,02H
MOV DL,ARRAY[DI]
INT 21H
SUB SI,1
LOOP LOOP5
MOV AH,02H
MOV DL,'H'
INT 21HStep by step
LOOPM:
DIV DIVNUM1; AX....DX
MOV RESULT1[SI],DL ; Although the remainder is placed in DX in , But the remainder will not exceed fifteen , therefore DH by 0
CMP AX,0
JZ T
MOV DX,0
ADD SI,1
LOOP LOOPMDIV DIVNUM1 Time to pay attention to , Divisor is DX When it comes to type , The divisor defaults to DX:AX, Shang Fang AX, The remainder is placed in DX in ; Divisor is DB When it comes to type , The divisor defaults to AX, Shang Fang AL, The remainder is placed in AH. So why let the divisor be DW What about the type? , Because, for example 6552 except 10 after , Business is 655, The remainder is 2,AL yes 8 position ,655 Out of range , There will be errors . So use DW Divisor of type
Cyclic redundancy , Put it in an array
If AX(AX What is preserved is the quotient ) by 0, Prove that all have been released , period , You can exit the loop , To the hexadecimal part of the output .
Note that at the end of each cycle MOV DX,0 , because DX Will do the high order of the dividend for a while , And the remainder just now is also placed in DX Inside
T:
ADD SI,1
MOV CX,SI ;CX As the number of cycles , Equals the subscript of the last number of the array plus 1
SUB SI,1
LOOP5:
MOV BL,RESULT1[SI]
MOV BH,0
MOV DI,BX
MOV AH,02H
MOV DL,ARRAY[DI]
INT 21H
SUB SI,1
LOOP LOOP5
MOV AH,02H
MOV DL,'H'
INT 21HOutput in reverse order , No difficulties
Bug
You cannot delete and re-enter a character after you enter it , When typing , For example, I input the wrong number ,554 I lost 555 了 , I can't change it .
Proposed solution
Use a string to accept the number entered by the user , For example, user input 6553, I just define a string , Put the user's input 65535 Put it in , Call another subroutine , Convert this string to a real number
Only the input part has changed
MOV COUNT,0
LOOP1:
CALL INPUT ; This is a very important question , It is here that I have a problem that I am not sure about normal input , Press enter , Press enter to save to NUM Inside
MOV SI,2
CMP NUM[SI],0DH ; Carriage return will be saved in NUM Position of the third byte of the array
JZ ENDINPUT
MOV SI,1
MOV CX,word ptr NUM[SI] ; take NUM True length of CX, As the number of cycles
SUB CX,1 ; Because the real length and the carriage return
ADD CX,1
MOV SI,CX ; These three lines are for , hold SI Positioning pair NUM The penultimate position of the array ( The last one is "enter" )
SUB CX,1
MOV BX,0
CALL TURN ; The string NUM Transform into something truly meaningful NUM
ADD COUNT,BX
LEA DX,CRLF
MOV AH,9
INT 21H
JMP LOOP1INPUT PROC
LEA DX,STR1
MOV AH,9
INT 21H
LEA DX,NUM
MOV AH,0AH
INT 21H
RET
INPUT ENDP
TURN PROC
PUSH AX
MOV TEMP,1
; I want to use BX For the time being NUM Real data in the array
LOOPN:
MOV AL,NUM[SI]
SUB AL,30H
MOV AH,0
MUL TEMP ;AX\*TEMP(DW)->DX:AX Because I know COUNT The maximum capacity is 16 position , So it must not be used DX
ADD BX,AX
SUB SI,1
MOV AX,TEMP
MUL MULNUM
MOV TEMP,AX
LOOP LOOPN
POP AX
RETThis method can basically succeed , But there is one problem that has not been solved , Every time I input a string , I am here DATA SEGMENT Only one string can be defined , Then the next input will overwrite this string . If the string entered this time , Shorter than last time , Then the actual length of the string is not accurate , And there will be more parts of the previous string that have not been overwritten .
So in general , Is how to empty the string .
If there is an error , Welcome to correct , Thank you for watching. !!!
边栏推荐
- [cross border e-commerce solutions] lighthouse will be used for pure IP expansion of new business - continuous efforts!
- The essence of five good books on wealth and freedom
- Quickly create a consumer cluster
- Open source stealing malware mercurial found in the field for "educational purposes"
- 5-rotating Daisy - rotating canvas and timer
- Deep learning ----- different methods to realize vgg16
- Vulnhub | dc: 4 | [actual combat]
- Object.defineProperty() 和 数据代理
- How to start Jupiter notebook in CONDA virtual environment
- 3-progressbar and secondary cropping
猜你喜欢

Vulnhub | DC: 4 |【实战】

Talk about the implementation principle of @autowired

Implementation of AVL tree

81 sentences worth repeating

Implementation principle and source code analysis of ThreadPoolExecutor thread pool

Linux Mysql安装

最常用的5中流ETL模式

The rtsp/onvif protocol video platform easynvr startup service reports an error "service not found". How to solve it?

The most commonly used 5-stream ETL mode

Object. Defineproperty() and data broker
随机推荐
Cloud computing "half peak"
Driver Architecture & platform platform bus driver model
Regular expression use cases
C # advanced learning -- virtual method
观察者模式
Implementation of AVL tree
How to solve the problem that flv video stream cannot be played and TS file generation fails due to packet loss?
Crawler frame
Talk about the implementation principle of @autowired
7-palette-calayer and touch
USB peripheral driver - configfs
Vulnhub | dc: 3 | [actual combat]
Deep analysis and Simulation of vector
点云库pcl从入门到精通 第十章
1-渐变、阴影和文本
【云计算】GFS思想优势以及架构
Self organizing map neural network (SOM)
Hongmeng reads the resource file
训练后的随机森林模型导出和加载
Vulnhub | DC: 4 |【實戰】