# Assembly language (input several decimal values (0-9) from the keyboard and display their sum in decimal)

Keywords: Assembly Language security

### subject

Receive several one digit decimal values (0-9) from the keyboard and display their sum in the form of decimal data.

requirement:
1) The input of one decimal value is realized by subroutine
2) When the user enters directly without entering a value, the input is ended
3) The input data is multi digit decimal data, and the sum calculated inside the machine is in hexadecimal form, which needs numerical conversion, and then the result is output in string form
4) Necessary prompt information is required in the program

Example: the user enters three values at the prompt
The sum is:12

## code

```DATA SEGMENT
STR1 DB "Please input a number: \$"
STR2 DB "The sum is: \$"
CRLF DB 0AH,0DH,'\$'   ;Line feed
COUNT DB 0
DIVNUM DB 10
RESULT DB 0,0,0,0,0,0,0,0
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
LOOP1:
CALL INPUT ;Subroutine call
CMP AL,0DH
JZ ENDINPUT
SUB AL,30H

LEA DX,CRLF ;Line feed
MOV AH,9
INT 21H
JMP LOOP1

ENDINPUT:
LEA DX,STR2 ;Line feed
MOV AH,9
INT 21H

MOV AL,COUNT
MOV AH,0 ;send AX=AL As divisor
MOV SI,0
LOOP2:
DIV DIVNUM   ; AL......AH
MOV RESULT[SI],AH
CMP AL,0
JZ NEXT
MOV AH,0
JMP LOOP2

NEXT:
MOV CX,SI
SUB SI,1

LOOP3:
MOV AH,02H
MOV DL,RESULT[SI]
INT 21H
SUB SI,1
LOOP LOOP3

MOV AX,4C00H
INT 21H

INPUT PROC
LEA DX,STR1
MOV AH,9
INT 21H

MOV AH,1
INT 21H

RET
INPUT ENDP
CODE ENDS
END START
```

## explain

### 1,

```LOOP1:
CALL INPUT ;Subroutine call
CMP AL,0DH
JZ ENDINPUT
SUB AL,30H

LEA DX,CRLF
MOV AH,9
INT 21H
JMP LOOP1
```

LOOP, but JMP is used. Because you don't know the number of cycles, and finally jump out of the LOOP according to enter, you can not use the LOOP command (there is only one exit in this LOOP, that is, when enter is input)
Subroutine calls CALL INPUT

```INPUT PROC
LEA DX,STR1
MOV AH,9
INT 21H

MOV AH,1
INT 21H

RET
```

The logic is simple, display the prompt STR1, and then enter the data
Register protection is not required here, because the CALL instruction is the first code, and no required registers are changed in the subroutine

CMP AL，0DH
Because the characters input in the subroutine call will be saved in AL, compare AL with the ASCII of carriage return. If carriage return, the cycle ends and the ENDINPUT jumps in

Otherwise, AL-30H will be used to convert the ASCII characters into real values, and COUNT will be used to save the cumulative sum

### 2,

```ENDINPUT:
LEA DX,STR2 ;Line feed
MOV AH,9
INT 21H

MOV AL,COUNT
MOV AH,0 ;send AX=AL As divisor
MOV SI,0
LOOP2:
DIV DIVNUM   ; AL......AH
MOV RESULT[SI],AH
CMP AL,0
JZ NEXT
MOV AH,0
JMP LOOP2
```

Now the real sum of all the input values has been obtained and saved in COUNT. Next, modulo 10 of COUNT one by one, each digit in COUNT in turn, and then saved in array RESULT, because modulo 10 first obtains one bit, ten bits... And finally needs to reverse output the array RESULT

```	MOV AL,COUNT
MOV AH,0 ;send AX=AL As divisor
MOV SI,0
```

The divisor defaults to AX. Because COUNT is of DB type, the value of COUNT is assigned to AX. Here, SI is used to control the position where the value is stored in the RESULT array

```LOOP2:
DIV DIVNUM   ; AL......AH
MOV RESULT[SI],AH
CMP AL,0
JZ NEXT
MOV AH,0
JMP LOOP2
```

Except for 10 remainder, the real value of the number is saved in the array now (not ASCII value. 30H should be added when outputting later. I think it should be OK to add it now, but I made an error when adding it here. I simply added it at the end)
Judge whether AL is 0. If AL is 0, it proves that each bit of all COUNT is put into the array, and exit the loop. If the loop does not continue, SI+1, the next number is placed in the next bit of the array. Remember to assign AH to 0 and the divisor is AX

### 3,

```NEXT:
MOV CX,SI
SUB SI,1

LOOP3:
MOV AH,02H
MOV DL,RESULT[SI]
INT 21H
SUB SI,1
LOOP LOOP3
```

The last step is to output the number in the array

```ADD SI,1
MOV CX,SI
SUB SI,1
```

Because SI is now the subscript of the last bit of the array, it needs + 1 to be the length of the array and assign CX as the number of cycles

```LOOP3: