8086 assembly-the user inputs any number between 1 and 100 and completes the decimal result output

The user inputs any number between 1 and 100, and the decimal result is output

The general idea is to read user input, process it into decimal numbers, and output it to the screen. First, the user’s input needs to be read. Initialize the memory and registers, clear the data in them, and read the first character

init: ;Initialization
    xor ax, ax ;clear
    xor bx, bx ;clear
    xor cx, cx ;clear
    xor dx, dx ;clear
    mov ah, 1 ; read a character mode
    int 21h ;interrupt

No illegal characters were read, reading in a loop

input: ;Start loop input
    cmp al, 30h; judge '0'
    jb check; smaller than 0, jump
    cmp al, 39h; judge '9'
    ja check; greater than 9, jump
    sub al, 30h;ASCII processing
    shl bx, 1 ;bx shift left
    mov cx, bx ;assignment
    shl bx, 1 ;bx shift left
    shl bx, 1 ;bx shift left
    add bx, cx ;Add
    add bl, al ;add
    mov ah, 1 ; read a character mode
    int 21h ;interrupt
    jmp input ;End of loop input

When you read an illegal character, check whether it is a carriage return. If yes, jump to Save Results section

check: ;Check input
    cmp al, 0dh ; Enter and press Enter
    je save; jump to save the result
    jmp init ; Input error, jump to initialization
save: ;Save the result
    mov ax, bx ;ax is the result
    pop dx; pop off the stack
    pop cx ; pop off the stack
    pop bx ; pop off the stack
    pop bp ;pop

Put the value in the register into the memory data segment

 mov num, ax; store in data segment memory
    push num ;push

Next is the output part. The hexadecimal number is still converted and decomposed, stored in the stack, and popped out of the stack in order to obtain the input number. Hexadecimal conversion part

divide: ;base conversion
    xor dx, dx ;clear
    div bx; divide by 10 to get the remainder, dx:ax / bx = ax...dx
    add dl, 30h ;ASCII conversion
    push dx; Business is pushed into the stack
    inc cx ;Number of digits + 1
    cmp ax, 0 ; quotient is 0, end
    jne divide ;quotient is not 0, continue dividing

Loop output

output: ;Start loop output
    pop dx; pop up the results one by one
    mov ah, 2 ;output mode
    int 21h ;interrupt
    loop output ;end of loop output

Complete code

stk segment
stk ends
data segment
    numdw?
data ends
code segment
    assume cs:code, ss:stk, ds:data
start:
    mov ax, data ;Data segment base address
    mov ds, ax ; assign value to base address register
;------------------------------------------------- ---------------
; Input part
    push bp; push onto stack
    mov bp, sp ; stack top pointer
    push bx ;bx is pushed onto the stack
    push cx ;cx pushed onto stack
    push dx ;dx push onto the stack
init: ;Initialization
    xor ax, ax ;clear
    xor bx, bx ;clear
    xor cx, cx ;clear
    xor dx, dx ;clear
    mov ah, 1 ; read a character mode
    int 21h ;interrupt
input: ;Start looping input
    cmp al, 30h; judge '0'
    jb check; smaller than 0, jump
    cmp al, 39h; judge '9'
    ja check; greater than 9, jump
    sub al, 30h;ASCII processing
    shl bx, 1 ;bx shift left
    mov cx, bx ;assignment
    shl bx, 1 ;bx shift left
    shl bx, 1 ;bx shift left
    add bx, cx ;Add
    add bl, al ;add
    mov ah, 1 ; read a character mode
    int 21h ;interrupt
    jmp input ;end of loop input
check: ;Check input
    cmp al, 0dh ; Enter and press Enter
    je save; jump to save the result
    jmp init ; Input error, jump to initialization
save: ;Save the result
    mov ax, bx ;ax is the result
    pop dx; pop off the stack
    pop cx ; pop off the stack
    pop bx ; pop off the stack
    pop bp; pop off the stack
; End of input section
;------------------------------------------------- ---------------
    mov num, ax; store in data segment memory
    push num; push onto stack
;------------------------------------------------- ---------------
; Output section
    push bp; push onto stack
    mov bp, sp ; stack top pointer
    push ax ;ax pushed onto the stack
    push bx ;bx is pushed onto the stack
    push cx ;cx pushed onto stack
    push dx ;dx push onto the stack
    xor cx, cx ;cx clear
    mov bx, [bp + 4] ; put num into bx
    mov ax, bx ;assignment
    mov bx, 10 ;decimal
divide: ;base conversion
    xor dx, dx ;clear
    div bx; divide by 10 to get the remainder, dx:ax / bx = ax...dx
    add dl, 30h ;ASCII conversion
    push dx; Business is pushed into the stack
    inc cx ;Number of digits + 1
    cmp ax, 0 ; quotient is 0, end
    jne divide ;The quotient is not 0, continue to divide
output: ;start loop output
    pop dx; pop up the results one by one
    mov ah, 2 ;output mode
    int 21h ;interrupt
    loop output; loop output ends
    pop dx; dx pops off the stack
    pop cx ;cx pops off the stack
    pop bx ;bx pops off the stack
    pop ax ;ax pops off the stack
    pop bp; bp pops off the stack
; End of output section
;------------------------------------------------- ---------------
    mov ah, 4ch
    int 21h
code ends
       end start

Q & amp;A

How to print the carriage return?

0DH is a line feed, 0AH is a carriage return, put this hexadecimal number into the ax register and execute an interrupt to input characters to the screen , you can complete the line break

How to distinguish between reading and writing?

01H is for writing, 02H is for reading. Put this hexadecimal number into the ax register and execute an interrupt. You can input characters to the computer. or output characters from computer

Experience

Through this homework exercise, I have deepened my understanding of loops and jumps, and become more familiar with the underlying registers and memory calls of the computer. I also reviewed the knowledge of base conversion.

Reference materials

  1. Use of gcc compiler

  2. data segment

  3. Assembly: 8086 memory management method and data addressing method

  4. Intel8086 processor-segment register ES/DS/CS/SS and addressing

  5. 8086 Assembly_Common Instructions

  6. 8086 assembly, decimal conversion to hexadecimal

  7. 8086 series (20): Hexadecimal to decimal conversion program

  8. 8086 assembly: input and output numbers, characters, and string functions

  9. 8086 assembly learning code segment, data segment, stack segment and segment address register

  10. Detailed explanation of the 14 registers in 8086CPU

  11. Assembly language (10) – conditional judgment instructions

  12. Assembly language–cmp instruction

  13. Assembly language CMP instructions

  14. Display 26 English letters”>Assembly->Display 26 English letters

  15. Assembly Language (3rd Edition)