├── .gitattributes ├── External Devices ├── thermometer.asm ├── LED_display_test.asm ├── traffic_lights.asm ├── keybrd.asm ├── traffic_lights2.asm ├── stepper_motor.asm ├── mouse.asm ├── robot.asm └── timer.asm ├── Introduction ├── 7_mov_instruction.asm ├── 6_keyboard_wait.asm ├── 8_define_byte.asm ├── 10_proc.asm ├── 11_proc_multiply.asm ├── 5_display_string.asm ├── 4_display_characters.asm ├── 2_hello_world_dos.asm ├── 3_hello_world_string.asm ├── 12_proc_hello_world.asm ├── 1_hello_world_VGA.asm ├── 9_hello_world_proc.asm ├── 13_interrupt_hello_world.asm └── 14_display_time.asm ├── Arithmetic ├── print_array.asm ├── lea.asm ├── daa.asm ├── offset.asm ├── multiply_8b.asm ├── add_16b_carry.asm ├── multiply_in_memory.asm ├── divide_16b_by_8b.asm ├── Sum_of_n_8b.asm ├── add_8b_16b.asm ├── find_largest.asm ├── add_in_memory.asm ├── add_16b_bcd.asm ├── count_1s.asm ├── multiply_2_32b.asm ├── add_8b_2.asm └── sub_8b.asm ├── Procedures └── myfirstproc.asm ├── Expression ├── reverse_array.asm ├── fibonacci3.asm ├── check_number_even_odd.asm ├── gcd_two.asm ├── average_sum_array.asm ├── fact.asm ├── sum_average_unsigned.asm ├── copy_string_memory_location.asm ├── fibonacci2.asm ├── power.asm ├── count_vowels.asm ├── substring_in_string.asm ├── fibonacci.asm ├── count_words.asm ├── given_number_prime.asm ├── finding_largest_number_memory.asm ├── concatenation_string.asm └── prompt_user_array+display.asm ├── Sorting ├── array_descending.asm ├── bubble_sort.asm └── array_ascending.asm ├── Searching ├── linear_search.asm ├── occurences_character_count.asm ├── search_element_array.asm └── binary_search.asm ├── Conversion ├── bcd_hex.asm ├── copy_string_instruction.asm ├── compare_strings.asm ├── copy_without_string_instruction.asm ├── hex_bcd.asm ├── hex_to_decimal.asm ├── seven_segment.asm ├── decimal_to_binary.asm ├── reverse_number.asm └── decimal_to_octal.asm ├── Simulation ├── water_level_controller.asm ├── fire_monitoring_system.asm └── garment_defect.asm └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /External Devices/thermometer.asm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jake1412/8086-Programs/HEAD/External Devices/thermometer.asm -------------------------------------------------------------------------------- /Introduction/7_mov_instruction.asm: -------------------------------------------------------------------------------- 1 | ORG 100H 2 | 3 | MOV AL,VAR1 4 | MOV BX,VAR2 5 | 6 | 7 | RET ; STOPS THE PROGRAM 8 | 9 | VAR1 DB 7 10 | VAR2 DW 1234H -------------------------------------------------------------------------------- /Introduction/6_keyboard_wait.asm: -------------------------------------------------------------------------------- 1 | org 100h 2 | 3 | main proc near 4 | mov ah,0h 5 | int 16h 6 | 7 | mov ax,4c00h 8 | int 21h 9 | endp 10 | end main 11 | 12 | ret -------------------------------------------------------------------------------- /Introduction/8_define_byte.asm: -------------------------------------------------------------------------------- 1 | ORG 100H 2 | 3 | DB 0A0H 4 | DB 08H 5 | DB 01H 6 | 7 | DB 8BH 8 | DB 1EH 9 | DB 09H 10 | DB 01H 11 | 12 | DB 0C3H 13 | 14 | DB 7 15 | 16 | DB 34H 17 | DB 12H -------------------------------------------------------------------------------- /Introduction/10_proc.asm: -------------------------------------------------------------------------------- 1 | ORG 100H 2 | 3 | CALL M1 4 | 5 | MOV AX,2 6 | 7 | RET ;return to operating system 8 | 9 | M1 PROC 10 | MOV BX,5 11 | RET ;return to caller 12 | M1 ENDP 13 | 14 | END -------------------------------------------------------------------------------- /Arithmetic/print_array.asm: -------------------------------------------------------------------------------- 1 | .data 2 | arr DB 50,51,52,53,54,55,56,57,48 3 | 4 | .code 5 | Main Proc 6 | mov cx, 9 7 | lea si, arr 8 | 9 | l1: 10 | mov dl, [si] 11 | mov ah, 2 12 | int 21h 13 | inc si 14 | loop l1 -------------------------------------------------------------------------------- /Arithmetic/lea.asm: -------------------------------------------------------------------------------- 1 | org 100h 2 | 3 | mov al,var1 ;check value of var1 by moving it to al 4 | 5 | lea bx,var1 ;get address of var1 in bx 6 | 7 | mov b.[bx],44h ;check value of var1 by moving it to al 8 | 9 | ret 10 | 11 | var1 db 22h 12 | 13 | end -------------------------------------------------------------------------------- /Introduction/11_proc_multiply.asm: -------------------------------------------------------------------------------- 1 | ORG 100H 2 | 3 | MOV AL,1 4 | MOV BL,2 5 | 6 | CALL M2 7 | CALL M2 8 | CALL M2 9 | CALL M2 10 | 11 | RET ;return to operating system 12 | 13 | M2 PROC 14 | MUL BL ;AX = AL*BL 15 | RET 16 | M2 ENDP 17 | 18 | END -------------------------------------------------------------------------------- /Arithmetic/daa.asm: -------------------------------------------------------------------------------- 1 | .model small 2 | .stack 200 3 | .data 4 | num1 db 27h 5 | num2 db 35h 6 | 7 | .code 8 | .startup 9 | mov al, num1 ;load ax with number num1 10 | add al, num2 ;al = al + num2 i.e. al = 5ch = 92 in decimal 11 | ;the expected result is 62 in decimal 12 | daa ; al = 62 13 | .exit 14 | end -------------------------------------------------------------------------------- /Arithmetic/offset.asm: -------------------------------------------------------------------------------- 1 | org 100h 2 | 3 | mov al,var1 ;check value of var1 by moving it to al 4 | 5 | mov bx,offset var1 ;get address of var1 in bx 6 | 7 | mov b.[bx],44h ;modify the contents of var1 8 | 9 | mov al,var1 ;check value of var1 by moving it to al 10 | 11 | ret 12 | 13 | var1 db 22h 14 | 15 | end -------------------------------------------------------------------------------- /Procedures/myfirstproc.asm: -------------------------------------------------------------------------------- 1 | ;program to create a simple multiplication procedure 2 | org 100h 3 | 4 | mov al,1h 5 | mov bl,2h 6 | 7 | call m2 8 | call m2 9 | call m2 10 | call m2 11 | 12 | ret ;return to the operating system 13 | 14 | m2 proc 15 | mul bl ;ax = al*bl 16 | ret ;return to caller 17 | m2 endp 18 | 19 | end -------------------------------------------------------------------------------- /Introduction/5_display_string.asm: -------------------------------------------------------------------------------- 1 | data segment 2 | message db "Hello World This is a test $" 3 | data ends 4 | 5 | code segment 6 | assume cs:code,ds:data 7 | start: mov ax,data 8 | mov ds,ax 9 | mov ah,09h 10 | lea dx,message 11 | int 21h 12 | 13 | mov ax,4c00h 14 | int 21h 15 | 16 | code ends 17 | end start -------------------------------------------------------------------------------- /Introduction/4_display_characters.asm: -------------------------------------------------------------------------------- 1 | code segment 2 | start: mov dl,'a' ;store ascii code of 'a' in dl 3 | mov ah,2h ;ms-dos character output function 4 | int 21h ;displays character in dl register 5 | 6 | mov ax,4c00h ;return to ms-dos 7 | int 21h ;do it! 8 | end start 9 | code ends -------------------------------------------------------------------------------- /Arithmetic/multiply_8b.asm: -------------------------------------------------------------------------------- 1 | data segment 2 | var1 db 0EDH 3 | var2 db 99H 4 | res dw ? 5 | data ends 6 | 7 | assume cs:code,ds:data 8 | 9 | code segment 10 | start: mov ax,data 11 | mov ds,ax 12 | 13 | mov al,var1 14 | mov bl,var2 15 | mul bl 16 | mov res,ax 17 | mov ah,4ch 18 | int 21h 19 | code ends 20 | end start -------------------------------------------------------------------------------- /Arithmetic/add_16b_carry.asm: -------------------------------------------------------------------------------- 1 | data segment 2 | var1 dw 1234h 3 | var2 dw 5140h 4 | result dw ? 5 | carry db 00h 6 | data ends 7 | 8 | code segment 9 | 10 | assume cs:code,ds:data 11 | 12 | mov ax,data 13 | mov ds,ax 14 | 15 | start: mov ax,var1 16 | add ax,var2 17 | jnc skip 18 | mov carry,01h 19 | 20 | skip: mov result,ax 21 | 22 | int 03h 23 | 24 | end start 25 | code ends 26 | end -------------------------------------------------------------------------------- /Expression/reverse_array.asm: -------------------------------------------------------------------------------- 1 | ; 8086 ASSEMBLY PROGRAMS TO FIND REVERSE OF AN ARRAY 2 | DATA SEGMENT 3 | STR1 DB 01H,02H,05H,03H,04H 4 | STR2 DB 5 DUP(?) 5 | DATA ENDS 6 | 7 | CODE SEGMENT 8 | ASSUME CS:CODE, DS:DATA 9 | START: 10 | MOV AX, DATA 11 | MOV DS, AX 12 | LEA SI, STR1 13 | LEA DI, STR2+4 14 | MOV CX, 05H 15 | 16 | BACK: CLD 17 | MOV AL, [SI] 18 | MOV [DI], AL 19 | INC SI 20 | DEC DI 21 | DEC CX 22 | JNZ BACK 23 | 24 | INT 3 25 | CODE ENDS 26 | END START 27 | -------------------------------------------------------------------------------- /Arithmetic/multiply_in_memory.asm: -------------------------------------------------------------------------------- 1 | ;multiply two 16-bit unsigned numbers stored at Memory locations 45,000H and 2 | ;45,002H. Store the result at 45,004H and 45,006H 3 | 4 | CODE SEGMENT 5 | assume cs:code 6 | 7 | mov ax,4000H 8 | mov ds,ax 9 | 10 | mov ax,[5000H] 11 | mov bx,[5002H] 12 | 13 | mul bx ;multiplies ax with bx 14 | 15 | mov [5004H],ax ;stores result in ax and dx 16 | mov [5006H],dx 17 | 18 | hlt 19 | CODE ENDS -------------------------------------------------------------------------------- /Introduction/2_hello_world_dos.asm: -------------------------------------------------------------------------------- 1 | ; Program to print Hello world using DOS interrupt function 2 | 3 | org 100h 4 | 5 | jmp start ;jump over data declaration 6 | 7 | msg: db "Hello,World!",0Dh,0Ah,24h 8 | 9 | start: mov dx,msg ;load offset of msg into dx 10 | mov ah,09h ;print function is 9 11 | int 21h ;do it! 12 | 13 | mov ah,0 14 | int 16h ;wait for any key.... 15 | 16 | ret ;return to operating system -------------------------------------------------------------------------------- /Sorting/array_descending.asm: -------------------------------------------------------------------------------- 1 | ;Array descending 2 | DATA SEGMENT 3 | STRING1 DB 99H,12H,56H,45H,36H 4 | DATA ENDS 5 | CODE SEGMENT 6 | ASSUME CS:CODE,DS:DATA 7 | START: MOV AX,DATA 8 | MOV DS,AX 9 | MOV CH,04H 10 | UP2: MOV CL,04H 11 | LEA SI,STRING1 12 | 13 | UP1:MOV AL,[SI] 14 | MOV BL,[SI+1] 15 | CMP AL,BL 16 | JNC DOWN 17 | MOV DL,[SI+1] 18 | XCHG [SI],DL 19 | MOV [SI+1],DL 20 | 21 | DOWN: INC SI 22 | DEC CL 23 | JNZ UP1 24 | DEC CH 25 | JNZ UP2 26 | INT 3 27 | CODE ENDS 28 | END START 29 | -------------------------------------------------------------------------------- /Arithmetic/divide_16b_by_8b.asm: -------------------------------------------------------------------------------- 1 | data segment 2 | var1 dw 6827H 3 | var2 db 0feH 4 | quo db ? 5 | rem db ? 6 | data ends 7 | 8 | assume cs:code,ds:data 9 | 10 | code segment 11 | start: mov ax,data 12 | mov ds,ax 13 | 14 | mov ax,var1 15 | mov bl,var2 16 | 17 | div bl 18 | 19 | mov quo,al 20 | mov rem,ah 21 | mov ah,4ch 22 | int 21h 23 | code ends 24 | end start -------------------------------------------------------------------------------- /Arithmetic/Sum_of_n_8b.asm: -------------------------------------------------------------------------------- 1 | data segment 2 | a db 1,2,3,4,5,6,7,8,9,10 3 | data ends 4 | 5 | code segment 6 | assume ds:data,cs:code 7 | 8 | start: 9 | mov ax,data 10 | mov ds,ax 11 | mov cl,10 12 | lea bx,a 13 | mov ah,00 14 | mov al,00 15 | 16 | l1: 17 | add al,byte ptr[bx] 18 | inc bx 19 | dec cl 20 | cmp cl,00 21 | jnz l1 22 | mov ah,4ch 23 | int 21h 24 | 25 | code ends 26 | 27 | end start -------------------------------------------------------------------------------- /Expression/fibonacci3.asm: -------------------------------------------------------------------------------- 1 | .model small 2 | .stack 3 | .data 4 | .code 5 | 6 | Mov ax, @data 7 | Mov ds, ax 8 | mov cx, 0000h 9 | Mov cl, 05h 10 | Mov al, 00h 11 | Mov bl, 01h 12 | Mov si, 0000h ;store the first value 0 in 0000h f(0) = 0 13 | Mov [si], al 14 | Inc si 15 | Mov [si], bl ;store the second value 1 in 0001h f(1) = 1 16 | 17 | Up: add al, bl ;add the two values f(n) = f(n-2) + f(n-1) for n>1 Inc si 18 | Mov [si], al ;store the third value in memory 19 | Xchg al, bl ; exchange the values for adding to generate series Loop up 20 | Int 21h 21 | End -------------------------------------------------------------------------------- /Searching/linear_search.asm: -------------------------------------------------------------------------------- 1 | ;linear search 2 | .8086 3 | .stack 100h 4 | 5 | .data 6 | a db 01h ,02h, 03h, 04h, 05h, 06h, 07h, 08h, 09h, 10h 7 | element db 05h ;element to be searched in the array a 8 | 9 | .code 10 | mov cx,10 11 | lea bx,a 12 | mov si,0 13 | mov dx,0 14 | label1: mov al,[bx+si] 15 | cmp al,element 16 | je equal 17 | inc si 18 | loop label1 19 | 20 | not_equal: mov dl,00h 21 | jmp exit 22 | 23 | equal: mov dl,0ffh 24 | 25 | exit: end -------------------------------------------------------------------------------- /Searching/occurences_character_count.asm: -------------------------------------------------------------------------------- 1 | ;An Assembly Language Program to search for a character in a given string and calculate the number 2 | ;of occurrences of the character in the given string 3 | DATA SEGMENT 4 | MSG1 DB 10,13,'ENTER ANY STRING :- $' 5 | MSG2 DB 10,13,'ENTER ANY CHARACTER :- $' 6 | MSG3 DB 10,13,' $' 7 | MSG4 DB 10,13,'NO, CHARACTER FOUND IN THE GIVEN STRING $' 8 | MSG5 DB ' CHARACTER(S) FOUND IN THE GIVEN STRING $' 9 | CHAR DB ? 10 | COUNT DB 0 11 | P1 LABEL BYTE 12 | M1 DB 0FFH 13 | L1 DB ? 14 | P11 DB 0FFH DUP ('$') 15 | DATA ENDS 16 | -------------------------------------------------------------------------------- /Expression/check_number_even_odd.asm: -------------------------------------------------------------------------------- 1 | ; check whether a number is even or odd 2 | data segment 3 | msg1 db 10,13,'enter number here :- $' 4 | msg2 db 10,13,'entered value is even$' 5 | msg3 db 10,13,'entered value is odd$' 6 | data ends 7 | display macro msg 8 | mov ah,9 9 | lea dx,msg 10 | int 21h 11 | endm 12 | code segment 13 | assume cs:code,ds:data 14 | start: 15 | mov ax,data 16 | mov ds,ax 17 | display msg1 18 | mov ah,1 19 | int 21h 20 | mov ah,0 21 | check: mov dl,2 22 | div dl 23 | cmp ah,0 24 | jne odd 25 | even: 26 | display msg2 27 | jmp done 28 | odd: 29 | display msg3 30 | done: 31 | mov ah,4ch 32 | int 21h 33 | code ends 34 | end start -------------------------------------------------------------------------------- /Searching/search_element_array.asm: -------------------------------------------------------------------------------- 1 | ;TO SEARCH AN ELEMENT IN AN ARRAY 2 | DATA SEGMENT 3 | STRING1 DB 11H,22H,33H,44H,55H 4 | MSG1 DB "FOUND$" 5 | MSG2 DB "NOT FOUND$" 6 | SE DB 33H 7 | DATA ENDS 8 | 9 | PRINT MACRO MSG 10 | MOV AH, 09H 11 | LEA DX, MSG 12 | INT 21H 13 | INT 3 14 | ENDM 15 | 16 | CODE SEGMENT 17 | ASSUME CS:CODE, DS:DATA 18 | START: 19 | MOV AX, DATA 20 | MOV DS, AX 21 | MOV AL, SE 22 | LEA SI, STRING1 23 | MOV CX, 04H 24 | 25 | UP: 26 | MOV BL,[SI] 27 | CMP AL, BL 28 | JZ FO 29 | INC SI 30 | DEC CX 31 | JNZ UP 32 | PRINT MSG2 33 | JMP END1 34 | 35 | FO: 36 | PRINT MSG1 37 | 38 | END1: 39 | INT 3 40 | CODE ENDS 41 | END START 42 | -------------------------------------------------------------------------------- /Introduction/3_hello_world_string.asm: -------------------------------------------------------------------------------- 1 | ; Hello world! 2 | 3 | data segment 4 | msg db "Hello,World!$" 5 | data ends 6 | 7 | code segment 8 | assume cs:code,ds:data 9 | start: mov ax,data ;intialize DS to address 10 | mov ds,ax ;of data segment 11 | lea dx,msg ;load effect address of message 12 | 13 | mov ah,09h ;display string function 14 | int 21h ;display message 15 | 16 | exit: mov ax,4c00h ;DOS function: Exit program 17 | int 21h ;Call DOS, terminate program 18 | code ends 19 | end start -------------------------------------------------------------------------------- /Conversion/bcd_hex.asm: -------------------------------------------------------------------------------- 1 | ;ALP for conversion of 16-bit BCD number into its equivalent HEX number. 2 | 3 | 4 | DATA SEGMENT 5 | BCD DB 06H,05H,05H,03H,05H 6 | HEX DW ? 7 | DATA ENDS 8 | 9 | ASSUME CS:CODE,DS:DATA 10 | 11 | CODE SEGMENT 12 | START: MOV AX,DATA 13 | MOV DS,AX 14 | MOV CL,05H 15 | MOV BP,000AH 16 | MOV AX,2710H 17 | PUSH AX 18 | MOV DI,0000H 19 | MOV SI, OFFSET BCD 20 | 21 | X: MOV BL,[SI] 22 | MUL BX 23 | ADD DI,AX 24 | POP AX 25 | DIV BP 26 | PUSH AX 27 | INC SI 28 | LOOP X 29 | MOV HEX,DI 30 | 31 | MOV AH,4CH 32 | INT 21H 33 | CODE ENDS 34 | END START -------------------------------------------------------------------------------- /Expression/gcd_two.asm: -------------------------------------------------------------------------------- 1 | ;ALP to find the Greatest Common Deviser of two unsigned integer. 2 | 3 | 4 | DATA SEGMENT 5 | NUM1 DW 0017H 6 | NUM2 DW 0007H 7 | GCD DW ? 8 | DATA ENDS 9 | 10 | ASSUME CS:CODE,DS:DATA 11 | 12 | CODE SEGMENT 13 | 14 | 15 | START: MOV AX,DATA 16 | MOV DS,AX 17 | MOV AX,NUM1 18 | MOV BX,NUM2 19 | 20 | 21 | X1: CMP AX,BX 22 | JE X4 23 | JB X3 24 | 25 | 26 | X2: MOV DX,0000H 27 | DIV BX 28 | CMP DX,0000H 29 | JE X4 30 | MOV AX,DX 31 | JMP X1 32 | 33 | 34 | X3: XCHG AX,BX 35 | JMP X2 36 | 37 | 38 | X4: MOV GCD ,BX 39 | MOV AH,4CH 40 | INT 21H 41 | 42 | CODE ENDS 43 | END START -------------------------------------------------------------------------------- /Arithmetic/add_8b_16b.asm: -------------------------------------------------------------------------------- 1 | data segment 2 | num1 db 05h 3 | num2 db 06h 4 | num3 dw 1234h 5 | num4 dw 0002h 6 | sum db ? 7 | sum2 dw ? 8 | data ends 9 | 10 | assume cs:code,ds:data 11 | 12 | code segment 13 | start: mov ax,data 14 | mov ds,ax ;initialize data segment 15 | 16 | mov al,num1 17 | add al,num2 ;add the 2 bytes 18 | 19 | mov sum,al ;stores result in memory 20 | 21 | mov cx,num3 22 | add cx,num4 ;add the 2 words 23 | 24 | mov sum2,cx ;stores the result in memory 25 | 26 | mov ah,4ch 27 | int 21h 28 | 29 | code ends 30 | end start -------------------------------------------------------------------------------- /External Devices/LED_display_test.asm: -------------------------------------------------------------------------------- 1 | ; this example shows how to access virtual ports (0 to 65535). 2 | ; these ports are emulated in this file: c:\emu8086.io 3 | 4 | ; this technology allows to make external add-on devices 5 | ; for emu8086, such as led displays, robots, thermometers, stepper-motors, etc... etc... 6 | 7 | ; anyone can create an animated virtual device. 8 | 9 | ; c:\emu8086\devices\led_display.exe 10 | 11 | #start=led_display.exe# 12 | 13 | 14 | #make_bin# 15 | 16 | name "led" 17 | 18 | mov ax, 1234 19 | out 199, ax 20 | 21 | mov ax, -5678 22 | out 199, ax 23 | 24 | ; Eternal loop to write 25 | ; values to port: 26 | mov ax, 0 27 | x1: 28 | out 199, ax 29 | inc ax 30 | jmp x1 31 | 32 | hlt 33 | 34 | 35 | -------------------------------------------------------------------------------- /Expression/average_sum_array.asm: -------------------------------------------------------------------------------- 1 | ;A program in assembly language to calculate the average of numbers in an array. 2 | DATA SEGMENT 3 | ARRAY DB 1,4,2,3,8,6,7,5,9 4 | AVG DB ? 5 | MSG DB "AVERAGE = $" 6 | ENDS 7 | 8 | CODE SEGMENT 9 | ASSUME DS:DATA CS:CODE 10 | START: 11 | MOV AX,DATA 12 | MOV DS,AX 13 | 14 | LEA SI,ARRAY 15 | LEA DX,MSG 16 | MOV AH,9 17 | INT 21H 18 | 19 | MOV AX,00 20 | MOV BL,9 21 | 22 | MOV CX,9 23 | LOOP1: 24 | ADD AL,ARRAY[SI] 25 | INC SI 26 | LOOP LOOP1 27 | 28 | DIV BL 29 | 30 | ADD AL,30H 31 | 32 | MOV DL,AL 33 | MOV AH,2 34 | INT 21H 35 | 36 | MOV AH,4CH 37 | INT 21H 38 | ENDS 39 | END START 40 | -------------------------------------------------------------------------------- /Expression/fact.asm: -------------------------------------------------------------------------------- 1 | ;Develop and execute an ALP to compute factorial of a positive integer 2 | ;number using recursive procedure. 3 | 4 | 5 | DATA SEGMENT 6 | NUM DW 0006H 7 | FACT DW ? 8 | DATA ENDS 9 | 10 | ASSUME CS:CODE,DS:DATA 11 | 12 | CODE SEGMENT 13 | 14 | START: MOV AX,DATA 15 | MOV DS,AX 16 | MOV AX,01H 17 | MOV BX,NUM 18 | CMP BX,0000H 19 | JZ X1 20 | CALL FACT1 21 | 22 | X1: MOV FACT,AX 23 | MOV FACT+2,DX 24 | 25 | MOV AH,4CH 26 | INT 21H 27 | 28 | FACT1 PROC 29 | CMP BX,01H 30 | JZ X 31 | PUSH BX 32 | DEC BX 33 | CALL FACT1 34 | POP BX 35 | MUL BX 36 | RET 37 | 38 | X: MOV AX,01H 39 | RET 40 | FACT1 ENDP 41 | 42 | CODE ENDS 43 | END START -------------------------------------------------------------------------------- /Arithmetic/find_largest.asm: -------------------------------------------------------------------------------- 1 | ;write a program to find the highest among 5 grades. 2 | 3 | data segment 4 | string1 db 13h,2ch,63h,58h,50h; 5 | data ends 6 | 7 | code segment 8 | assume cs:code, ds:data 9 | start: mov ax, data 10 | mov ds, ax 11 | mov cx, 04h ;set up loop counter 12 | 13 | mov bl, 00h ;bl stores highest grade 14 | lea si, string1 ;si points to first grade 15 | 16 | up: mov al, [si] ;compare next grade to highest 17 | cmp al, bl 18 | jl nxt ;jump if al is still the highest 19 | mov bl, al ;else bl holds new highest 20 | 21 | nxt: inc si ;point to next grade 22 | dec cx 23 | jnz up 24 | 25 | mov dl,bl 26 | 27 | code ends 28 | end start -------------------------------------------------------------------------------- /Expression/sum_average_unsigned.asm: -------------------------------------------------------------------------------- 1 | ;ALP to find the Sum and average of unsigned integer. 2 | 3 | 4 | DATA SEGMENT 5 | A DW 1234H,3223H,0FFFFH,4326H,0FEF3H,4325H 6 | N DW 0006H 7 | SUM DW 2 DUP(0) 8 | AVG DW ? 9 | DATA ENDS 10 | 11 | 12 | ASSUME CS:CODE,DS:DATA 13 | 14 | CODE SEGMENT 15 | 16 | START: MOV AX,DATA 17 | MOV DS,AX 18 | MOV SI,0000H 19 | MOV DX,0000H 20 | MOV CX,N 21 | MOV AX,0000H 22 | CLC 23 | 24 | X: ADD AX,A[SI] 25 | JC K 26 | 27 | X1: INC SI 28 | INC SI 29 | LOOP X 30 | JMP QUIT 31 | 32 | K: ADD DX,0001H 33 | JMP X1 34 | 35 | QUIT: MOV SUM,AX 36 | MOV SUM+2,DX 37 | DIV N 38 | MOV AVG,AX 39 | MOV AH,4CH 40 | INT 21H 41 | 42 | CODE ENDS 43 | END START -------------------------------------------------------------------------------- /Conversion/copy_string_instruction.asm: -------------------------------------------------------------------------------- 1 | ;ALP to copy the string of successive memory locations from one memory to 2 | ;other 3 | ;Using string instructions 4 | 5 | 6 | DATA SEGMENT 7 | SOURCE DB "BIOMEDICAL" 8 | DATA ENDS 9 | 10 | EXTRA SEGMENT 11 | DEST DB ? 12 | EXTRA ENDS 13 | 14 | CODE SEGMENT 15 | ASSUME CS:CODE , DS:DATA, ES:EXTRA 16 | START : MOV AX,DATA 17 | MOV DS,AX 18 | MOV AX,EXTRA 19 | MOV ES,AX 20 | MOV SI,00H 21 | MOV DI,00H 22 | CLD 23 | MOV CX,000AH 24 | REP MOVSB 25 | 26 | X: MOV AL,SOURCE [SI] 27 | MOV DEST [DI],AL 28 | INC SI 29 | INC DI 30 | LOOP X 31 | 32 | MOV AH,4CH 33 | INT 21H 34 | CODE ENDS 35 | END START -------------------------------------------------------------------------------- /Arithmetic/add_in_memory.asm: -------------------------------------------------------------------------------- 1 | ;add a series of 10 bytes stores in the memory from locations 2 | ;20,000H to 20,009. Store the result immediately after the series. 3 | 4 | CODE SEGMENT 5 | assume CS:Code 6 | 7 | mov ax,2000H 8 | mov ds,ax 9 | 10 | mov si,0000H ;starting offset address = 0000H 11 | mov cx,000AH ;10 bytes = 'A' 12 | mov ax,0000H ;ax initialized to store sum 13 | 14 | back: add al,[si] ;indirect register addressing 15 | jnc skip ;jump if no carry 16 | inc ah ;if sum is greater than 8 bit 17 | skip: inc si ;point to next offset 18 | loop back ;decrement cx by 1 19 | 20 | mov [si],ax ;store final result in 20,009H 21 | 22 | HLT 23 | CODE ENDS -------------------------------------------------------------------------------- /Sorting/bubble_sort.asm: -------------------------------------------------------------------------------- 1 | ;ALP to Sort a set of unsigned integer numbers in ascending/ descending 2 | ;order using Bubble sort algorithm. 3 | 4 | 5 | DATA SEGMENT 6 | A DW 0005H, 0ABCDH, 5678H, 1234H, 0EFCDH, 45EFH 7 | DATA ENDS 8 | 9 | ASSUME CS:CODE,DS:DATA 10 | 11 | CODE SEGMENT 12 | START: MOV AX,DATA 13 | MOV DS,AX 14 | MOV SI,0000H 15 | MOV BX,A[SI] 16 | DEC BX 17 | 18 | X2: MOV CX,BX 19 | MOV SI,02H 20 | 21 | X1: MOV AX,A[SI] 22 | INC SI 23 | INC SI 24 | CMP AX,A[SI] 25 | JA X3 26 | XCHG AX,A[SI] 27 | MOV A[SI-2],AX 28 | 29 | X3: LOOP X1 30 | DEC BX 31 | JNZ X2 32 | 33 | MOV AH,4CH 34 | INT 21H 35 | CODE ENDS 36 | END START 37 | -------------------------------------------------------------------------------- /Arithmetic/add_16b_bcd.asm: -------------------------------------------------------------------------------- 1 | data segment 2 | var1 dw 9348H 3 | var2 dw 1845H 4 | result dw ? 5 | carry dw 00H 6 | data ends 7 | 8 | code segment 9 | assume cs:code,ds:data 10 | start: mov ax,data 11 | mov ds,ax 12 | 13 | mov ax,var1 14 | mov bx,var2 15 | 16 | add al,bl 17 | daa ;decimal adjust after addition 18 | mov cl,al 19 | 20 | mov al,ah ;since daa works on al register 21 | add al,bh 22 | daa 23 | mov ch,al 24 | 25 | jnc skip ;jump to skip if carry flag is 0 26 | mov carry,01H 27 | 28 | skip: mov result,cx 29 | 30 | int 03H 31 | 32 | end start 33 | code ends 34 | -------------------------------------------------------------------------------- /Conversion/compare_strings.asm: -------------------------------------------------------------------------------- 1 | data SEGMENT 2 | p_str1 DB Cr, Lf, 'Enter 1st string: ',0 3 | p_str2 DB Cr, Lf, 'Enter 2nd string: ',0 4 | p_not DB Cr, Lf, 'The strings are not same',0 5 | p_same DB Cr, Lf, 'The strings are the same',0 6 | str1 DB 100 DUP (?) 7 | str2 DB 100 DUP (?) 8 | data ENDS 9 | 10 | code SEGMENT 11 | ASSUME cs:code, ds:data 12 | start: mov ax, data 13 | mov ds, ax 14 | ;input str1 15 | output p_str1 16 | inputs str1, 100 17 | ;input str2 18 | output p_str2 19 | inputs str2, 100 20 | ;initialize 21 | lea si, str1 22 | lea di, str2 23 | ;use string instructions 24 | ;to comapre the two strings 25 | cld;clear direction flags 26 | repe cmpsb;repeat compare string byte 27 | je p_same 28 | jmp p_not 29 | 30 | l_not: output p_not 31 | jmp quit 32 | l_same: output p_same 33 | 34 | quit: mov al, 00h 35 | mov ah, 4ch 36 | int 21h 37 | code ENDS 38 | END start 39 | -------------------------------------------------------------------------------- /Conversion/copy_without_string_instruction.asm: -------------------------------------------------------------------------------- 1 | ;ALP to copy the string of successive memory locations from one memory to 2 | ;other 3 | ;Without using string instructions 4 | 5 | 6 | DATA SEGMENT 7 | SOURCE DB "BIOMEDICAL" 8 | DATA ENDS 9 | 10 | EXTRA SEGMENT 11 | DEST DB ? 12 | EXTRA ENDS 13 | 14 | CODE SEGMENT 15 | ASSUME CS:CODE ,DS:DATA,ES:EXTRA 16 | START : MOV AX,DATA 17 | MOV DS,AX 18 | MOV AX,EXTRA 19 | MOV ES,AX 20 | 21 | MOV SI,00H 22 | MOV DI,00H 23 | MOV SI,OFFSET SOURCE 24 | MOV DI,OFFSET DEST 25 | MOV CX,000AH 26 | 27 | X: MOV AL,SOURCE [SI] 28 | MOV DEST [DI],AL 29 | INC SI 30 | INC DI 31 | LOOP X 32 | 33 | MOV AH,4CH 34 | INT 21H 35 | CODE ENDS 36 | END START -------------------------------------------------------------------------------- /External Devices/traffic_lights.asm: -------------------------------------------------------------------------------- 1 | ;controlling external device with 8086 microprocessor 2 | ;realistic test for c:\emu8086\devices\Traffic_Lights.exe 3 | 4 | #start=Traffic_Lights.exe 5 | 6 | name "traffic" 7 | 8 | mov ax,all_red 9 | out 4,ax 10 | 11 | lea si,situation 12 | 13 | next: mov ax,[si] 14 | out 4,ax 15 | 16 | ;wait for 5 seconds (5 million microseconds) 17 | 18 | mov cx,4ch ;004C4B40H = 5,000,000 19 | mov dx,4b40h 20 | mov ah, 86h 21 | int 15h 22 | 23 | add si,2 ;next situation 24 | cmp si,sit_end 25 | jb next 26 | 27 | lea si,situation 28 | jmp next 29 | 30 | 31 | ; FEDC_BA98_7654_3210 32 | situation dw 0000_0011_0000_1100b 33 | s1 dw 0000_0110_1001_1010b 34 | s2 dw 0000_1000_0110_0001b 35 | s3 dw 0000_1000_0110_0001b 36 | s4 dw 0000_0100_1101_0011b 37 | sit_end = $ 38 | all_red equ 0000_0010_0100_1001b -------------------------------------------------------------------------------- /Conversion/hex_bcd.asm: -------------------------------------------------------------------------------- 1 | ;ALP for conversion of 16-bit HEX number into its equivalent BCD number. 2 | 3 | 4 | DATA SEGMENT 5 | HEX DW 0FFFFH 6 | BCD DW 5 DUP(0) 7 | DATA ENDS 8 | 9 | ASSUME CS:CODE,DS:DATA 10 | 11 | CODE SEGMENT 12 | 13 | START: MOV AX,DATA ;intialize data 14 | MOV DS,AX 15 | 16 | LEA SI,BCD 17 | MOV AX,HEX 18 | 19 | MOV CX,2710H 20 | CALL SUB1 21 | 22 | MOV CX,03E8H 23 | CALL SUB1 24 | 25 | MOV CX,0064H 26 | CALL SUB1 27 | 28 | MOV CX,000AH 29 | CALL SUB1 30 | 31 | MOV [SI],AL 32 | 33 | MOV AH,4CH 34 | INT 21H 35 | 36 | SUB1 PROC NEAR 37 | MOV BH,0FFH 38 | X1: INC BH 39 | SUB AX,CX 40 | JNC X1 41 | ADD AX,CX 42 | MOV [SI] ,BH 43 | INC SI 44 | RET 45 | SUB1 ENDP 46 | 47 | CODE ENDS 48 | END START 49 | -------------------------------------------------------------------------------- /Conversion/hex_to_decimal.asm: -------------------------------------------------------------------------------- 1 | ;An Assembly program in which a procedure converts Hexadecimal value to print its Decimal form ;on Screen 2 | DATA SEGMENT 3 | NUM DW 1234H 4 | RES DB 10 DUP ('$') 5 | DATA ENDS 6 | CODE SEGMENT 7 | ASSUME DS:DATA,CS:CODE 8 | START: 9 | MOV AX,DATA 10 | MOV DS,AX 11 | 12 | MOV AX,NUM 13 | 14 | LEA SI,RES 15 | CALL HEX2DEC 16 | 17 | LEA DX,RES 18 | MOV AH,9 19 | INT 21H 20 | 21 | MOV AH,4CH 22 | INT 21H 23 | CODE ENDS 24 | HEX2DEC PROC NEAR 25 | MOV CX,0 26 | MOV BX,10 27 | 28 | LOOP1: MOV DX,0 29 | DIV BX 30 | ADD DL,30H 31 | PUSH DX 32 | INC CX 33 | CMP AX,9 34 | JG LOOP1 35 | 36 | ADD AL,30H 37 | MOV [SI],AL 38 | 39 | LOOP2: POP AX 40 | INC SI 41 | MOV [SI],AL 42 | LOOP LOOP2 43 | RET 44 | HEX2DEC ENDP 45 | 46 | END START 47 | -------------------------------------------------------------------------------- /Introduction/12_proc_hello_world.asm: -------------------------------------------------------------------------------- 1 | ORG 100H 2 | 3 | LEA SI,msg ;load effective address 4 | 5 | CALL print_me 6 | 7 | RET ;return to operating system 8 | 9 | ;================================================== 10 | ;this procedure prints a string should be null 11 | ;terminated (have zero in the end), 12 | ;the string address should be in SI register: 13 | 14 | print_me PROC 15 | 16 | next_char: 17 | CMP b.[SI],0 ;check for zero to stop 18 | JE stop ; 19 | 20 | MOV AL,[SI] ;next get ASCII char 21 | 22 | MOV AH,0EH ;teletype function number 23 | INT 10H ;using interrupt to print a char in AL 24 | 25 | ADD SI,1 ;advance index of string array 26 | 27 | JMP next_char 28 | 29 | stop: 30 | RET ;return to caller 31 | print_me ENDP 32 | 33 | ;=================================================== 34 | 35 | msg DB 'Hello World!',0 ;null terminated string 36 | 37 | END -------------------------------------------------------------------------------- /Expression/copy_string_memory_location.asm: -------------------------------------------------------------------------------- 1 | ; Code for PROGRAM TO TRANSFER A STRING FROM ONE MEMORY LOCATION TO ANOTHER in Assembly Language 2 | READ MACRO MSG 3 | MOV AH,0AH 4 | LEA DX,MSG 5 | INT 21H 6 | ENDM 7 | 8 | PRINT MACRO MSG 9 | MOV AH,09H 10 | LEA DX,MSG 11 | INT 21H 12 | ENDM 13 | 14 | DATA SEGMENT 15 | CR EQU 0DH 16 | LF EQU 0AH 17 | MSG1 DB "ENTER STRING:$" 18 | MSG2 DB "CONTENTS ARE TRANSFERRED $" 19 | BUFF DB 255 20 | DB 0 21 | DB 255 DUP ('$') 22 | DEST DB 255 DUP ('$') 23 | DATA ENDS 24 | 25 | CODE SEGMENT 26 | ASSUME CS:CODE,DS:DATA 27 | START: MOV AX,DATA 28 | MOV DS,AX 29 | PRINT MSG1 30 | READ BUFF 31 | MOV SI,OFFSET BUFF+2 32 | MOV DI,OFFSET BUFF 33 | MOV CL,BYTEPTR[SI+1] 34 | MOV CH,00H 35 | CLD 36 | 37 | REPEAT: MOVSB 38 | PRINT MSG2 39 | PRINT DEST 40 | MOV AH,4CH 41 | INT 21H 42 | 43 | CODE ENDS 44 | 45 | END START 46 | -------------------------------------------------------------------------------- /Sorting/array_ascending.asm: -------------------------------------------------------------------------------- 1 | ;Code for An Assembly Language Program sort a given series in ascending order in Assembly ;Language 2 | Data Segment 3 | arr1 db 8,2,7,4,3 4 | Data Ends 5 | 6 | Code Segment 7 | Assume cs:code, ds:data 8 | 9 | Begin: 10 | mov ax, data 11 | mov ds, ax 12 | mov es, ax 13 | mov bx, OFFSET arr1 14 | mov cx, 5 15 | mov dx, cx 16 | L1: 17 | mov si, 0 18 | mov ax, si 19 | inc ax 20 | mov di, ax 21 | mov dx, cx 22 | L2: 23 | mov al, [bx][si] 24 | cmp al, [bx][di] 25 | jg L4 26 | L3: 27 | inc si 28 | inc di 29 | dec dx 30 | cmp dx, 00 31 | je L1 32 | jg L2 33 | L4: 34 | mov al, [bx][si] 35 | mov ah, [bx][di] 36 | mov [bx][si], ah 37 | mov [bx][di], al 38 | inc si 39 | inc di 40 | dec dx 41 | cmp dx, 00 42 | je L1 43 | jg L2 44 | Exit: 45 | mov ax, 4c00h 46 | int 21h 47 | Code Ends 48 | End Begin 49 | -------------------------------------------------------------------------------- /Expression/fibonacci2.asm: -------------------------------------------------------------------------------- 1 | ;Name: fibLoop 2 | ;Purpose: First 5 Fib Terms 3 | ;On Entry: 4 | ; Depends on CX as a counter ;On Exit 5 | ; None. 6 | ;Process: 7 | ; Step 01 - Offset DL 8 | ; Step 02 - Display 9 | ; Step 03 - Save DX current state 10 | 11 | 12 | ; Step04-AL=DL 13 | ; Step05-AH=DH 14 | ; Step06-AH=AL 15 | ; Step07-BH=AH 16 | ; Step 08 - Returns DX 17 | ; Step09-Loop 18 | 19 | org 100h 20 | lea DX, msg1 21 | mov AH, 09h 22 | int 21h 23 | mov DL, 20h 24 | mov AH, 02h 25 | int 21h 26 | mov BH, 1 27 | mov DH, 1 28 | mov CX, 5 29 | fibLoop: or DL, 30h 30 | mov AH, 02h 31 | int 21h 32 | 33 | mov DL, DH 34 | mov DH, BH 35 | push DX 36 | mov AL, DL 37 | mov AH, DH 38 | add AH, AL 39 | mov BH, AH 40 | pop DX 41 | loop fibLoop 42 | ret 43 | 44 | msg1 db "FIBONACCI FIRST 5 TERMS",0Dh,0AH 45 | db "I start from 0 while others like", 0Dh,0AH 46 | db "to include the 1 in the first term.", 0Dh,0AH, "$" 47 | fibTerm db 12 Dup(0) 48 | -------------------------------------------------------------------------------- /Conversion/seven_segment.asm: -------------------------------------------------------------------------------- 1 | ;ALP for conversion BCD number 7-Segment 2 | 3 | 4 | DATA SEGMENT 5 | TABLE DB 7EH, 30H, 60H, 79H, 33H, 5BH, 5FH, 70H, 7FH, 73H ; look up table for translating 6 | A DB 09H,01H,06H ; 7 - segment numbers to be translated 7 | B DB ? 8 | DATA ENDS 9 | 10 | CODE SEGMENT 11 | 12 | ASSUME CS:CODE,DS:DATA 13 | START: MOV AX,DATA 14 | MOV DS,AX 15 | MOV ES,AX 16 | LEA BX,TABLE ; load address of look up table into bx 17 | LEA SI,A 18 | LEA DI,B 19 | MOV CX,03H ; 3 numbers to be translated 20 | CLD 21 | 22 | X1: MOV AL,[SI] ; load string into AL register 23 | CMP AL,09H 24 | JA X2 ; exit if number is over 9 25 | 26 | XLAT 27 | MOV [DI],AX ; stores string into B 28 | INC DI 29 | INC SI 30 | LOOP X1 31 | 32 | X2: MOV AH,4CH 33 | INT 21H 34 | CODE ENDS 35 | END START -------------------------------------------------------------------------------- /Expression/power.asm: -------------------------------------------------------------------------------- 1 | ; Code for Program to Calculate power(a,b) i.e a^b in Assembly Language 2 | .MODEL SMALL 3 | .DATA 4 | BASE DB ? 5 | POW DB ? 6 | NL1 DB 0AH,0DH,'ENTER BASE:','$' 7 | NL2 DB 0AH,0DH,'ENTER POWER:','$' 8 | 9 | .CODE 10 | MAIN PROC 11 | 12 | MOV AX,@DATA 13 | MOV DS,AX 14 | 15 | ENTER_BASE: 16 | LEA DX,NL1 17 | MOV AH,09H 18 | INT 21H 19 | 20 | MOV AH,01H 21 | INT 21H 22 | SUB AL,30H 23 | MOV BL,AL 24 | 25 | MOV BASE,AL 26 | 27 | ENTER_POWER: 28 | LEA DX,NL2 29 | MOV AH,09H 30 | INT 21H 31 | 32 | MOV AH,01H 33 | INT 21H 34 | SUB AL,30H 35 | 36 | MOV CL,AL 37 | DEC CL 38 | MOV AX,00 39 | MOV AL,BASE 40 | 41 | LBL1: 42 | MUL BL 43 | LOOP LBL1 44 | 45 | MOV CL,10 46 | DIV CL 47 | ADD AX,3030H 48 | MOV DX,AX 49 | 50 | MOV AH,02H 51 | INT 21H 52 | MOV DL,DH 53 | INT 21H 54 | 55 | MOV AH,4CH 56 | INT 21H 57 | 58 | MAIN ENDP 59 | END MAIN 60 | -------------------------------------------------------------------------------- /Conversion/decimal_to_binary.asm: -------------------------------------------------------------------------------- 1 | ;Code for Program to convert decimal number to binary in Assembly Language 2 | DIS MACRO STR 3 | MOV AH,09H 4 | LEA DX,STR 5 | INT 21H 6 | ENDM 7 | DATA SEGMENT 8 | MSG2 DB "BINARY NUMBER IS : $" 9 | STR1 DB 20 DUP('$') 10 | STR2 DB 20 DUP('$') 11 | NO DW 100 12 | LINE DB 10,13,'$' 13 | DATA ENDS 14 | 15 | CODE SEGMENT 16 | ASSUME DS:DATA,CS:CODE 17 | START: 18 | MOV AX,DATA 19 | MOV DS,AX 20 | LEA SI,STR1 21 | MOV AX,NO 22 | MOV BH,00 23 | MOV BL,2 24 | L1:DIV BL 25 | ADD AH,'0' 26 | MOV BYTE PTR[SI],AH 27 | MOV AH,00 28 | INC SI 29 | INC BH 30 | CMP AL,00 31 | JNE L1 32 | 33 | MOV CL,BH 34 | LEA SI,STR1 35 | LEA DI,STR2 36 | MOV CH,00 37 | ADD SI,CX 38 | DEC SI 39 | 40 | L2:MOV AH,BYTE PTR[SI] 41 | MOV BYTE PTR[DI],AH 42 | DEC SI 43 | INC DI 44 | LOOP L2 45 | 46 | DIS LINE 47 | DIS MSG2 48 | DIS STR2 49 | MOV AH,4CH 50 | INT 21H 51 | CODE ENDS 52 | END START 53 | -------------------------------------------------------------------------------- /Searching/binary_search.asm: -------------------------------------------------------------------------------- 1 | ;Develop and execute ALP that implements Binary search algorithm. The data 2 | ;consists of sorted 16 bit unsigned integers. The search key is also a 16 bit unsigned 3 | ;integer. 4 | 5 | 6 | DATA SEGMENT 7 | ARR DW 05H,0111H,2161H,4541H,7161H,8231H 8 | SR EQU 4541H 9 | MSG1 DB 'ELEMENT FOUND AT ' 10 | RES DB ' RD POSITION','$' 11 | MSG2 DB 'ELEMENT NOT FOUND','$' 12 | DATA ENDS 13 | 14 | ASSUME CS:CODE,DS:DATA 15 | 16 | CODE SEGMENT 17 | START: MOV AX,DATA 18 | MOV DS,AX 19 | MOV BX,00H 20 | MOV CX,SR 21 | MOV DX,05H 22 | 23 | LP: CMP BX,DX 24 | JA FAILURE 25 | MOV AX,BX 26 | ADD AX,DX 27 | SHR AX,01 28 | MOV SI,AX 29 | ADD SI,SI 30 | CMP CX,ARR[SI] 31 | JAE BIGGER 32 | DEC AX 33 | MOV DX,AX 34 | JMP LP 35 | 36 | BIGGER: JE SUCCESS 37 | INC AX 38 | MOV BX,AX 39 | JMP LP 40 | 41 | SUCCESS:ADD AL,01H 42 | ADD AL,2FH 43 | MOV RES,AL 44 | LEA DX,MSG1 45 | JMP DISPLAY 46 | 47 | FAILURE: LEA DX,MSG2 48 | DISPLAY: MOV AH,09H 49 | INT 21H 50 | MOV AH,4CH 51 | INT 21H 52 | 53 | CODE ENDS 54 | END START -------------------------------------------------------------------------------- /Expression/count_vowels.asm: -------------------------------------------------------------------------------- 1 | ;8086 PROGRAM: COUNT NUMBER OF VOWELS IN GIVEN LINE OF A TEXT/SENTENCE 2 | .MODEL SMALL 3 | 4 | .STACK 100H 5 | 6 | .DATA 7 | STRING DB 10,13,"The quick brown fox jumped over lazy sleeping dog$" 8 | VOWEL DB ? 9 | MSG1 DB 10,13,"Number of vowels are: $" 10 | 11 | .CODE 12 | MAIN PROC 13 | MOV AX, @DATA 14 | MOV DS, AX 15 | MOV SI, OFFSET STRING 16 | MOV BL, 00 17 | 18 | BACK: MOV AL, [SI] 19 | CMP AL,'$' 20 | JZ FINAL 21 | CMP AL,'A' 22 | JZ COUNT 23 | CMP AL,'E' 24 | JZ COUNT 25 | CMP AL,'I' 26 | JZ COUNT 27 | CMP AL,'O' 28 | JZ COUNT 29 | CMP AL,'U' 30 | JZ COUNT 31 | CMP AL,'a' 32 | JZ COUNT 33 | CMP AL,'e' 34 | JZ COUNT 35 | CMP AL,'i' 36 | JZ COUNT 37 | CMP AL,'o' 38 | JZ COUNT 39 | CMP AL,'u' 40 | JZ COUNT 41 | INC SI 42 | JMP BACK 43 | 44 | COUNT: INC BL 45 | INC SI 46 | JMP BACK 47 | 48 | FINAL: MOV AH,2H 49 | MOV DL,BL 50 | INT 21H ;print number of vowels 51 | 52 | MOV AH, 4CH 53 | INT 21H 54 | MAIN ENDP 55 | END 56 | -------------------------------------------------------------------------------- /Introduction/1_hello_world_VGA.asm: -------------------------------------------------------------------------------- 1 | ORG 100H ; set video mode 2 | mov ax,3 ; text mode 80x25, 16 colors, 8 pages(ah=0,al=3) 3 | int 10h ; interrupt for screen manipulation 4 | 5 | mov ax,1003h ; enable bold background (16 colors) and cancel blinking 6 | mov bx, 0 7 | int 10h 8 | 9 | ;set segment register 10 | mov ax,0b800h ; address of VGA card for text mode 11 | mov ds,ax ; move ax into ds, so ds holds the address of VGA card 12 | 13 | ;print "Hello World!" 14 | ;first byte is ascii code, second byte is color code 15 | 16 | mov [02h],'H' 17 | 18 | mov [04h],'e' 19 | 20 | mov [06h],'l' 21 | 22 | mov [08h],'l' 23 | 24 | mov [0ah],'o' 25 | 26 | mov [0ch],',' 27 | 28 | mov [0eh],'W' 29 | 30 | mov [10h],'o' 31 | 32 | mov [12h],'r' 33 | 34 | mov [14h],'l' 35 | 36 | mov [16h],'d' 37 | 38 | mov [18h],'!' 39 | 40 | 41 | ;color all characters 42 | mov cx,12 ; counter register set to number of characters 43 | mov di,03h ; start from byte after 'h' 44 | 45 | c: mov [di],11101100b ; light red (1100) on yellow (1110) 46 | add di,2 ; increment di value by 2 to skip over to next ascii code in VGA memory 47 | loop c 48 | 49 | mov ah,0 50 | int 16h ;wait for any key press, keyboard interrupt 51 | 52 | ret -------------------------------------------------------------------------------- /Arithmetic/count_1s.asm: -------------------------------------------------------------------------------- 1 | ;program to count number of binary 1s 2 | data segment 3 | data1 db ? 4 | msg1 db 10,13,"enter the number: $" 5 | msg3 db 10,13,"number of 1s are: $" 6 | data ends 7 | 8 | assume cs:code,ds:data 9 | code segment 10 | start: mov ax,data 11 | mov ds,ax 12 | sub bl,bl 13 | 14 | lea dx,msg1 ;load address of msg1 into dx 15 | mov ah,9h ;interrupt to display contents of dx 16 | int 21h 17 | 18 | mov ah,1h ;read a character from console 19 | int 21h 20 | sub al,30h ;converting from ascii into bcd form 21 | 22 | mov dl,8h ;set up count register 23 | again: rol al,1 24 | jnc next ;conditional jump if carry flag is 0 25 | inc bl ;number of 1s 26 | next: dec dl 27 | jnz again ;short jump if del is not zero 28 | 29 | lea dx,msg3 ;print msg3 30 | mov ah,9h 31 | int 21h 32 | 33 | mov ah,2h ;print number of 1s 34 | add bl,30h 35 | mov dl,bl 36 | int 21h 37 | 38 | exit: mov ah,04ch 39 | mov al,0 40 | int 21h 41 | 42 | code ends 43 | end start -------------------------------------------------------------------------------- /Expression/substring_in_string.asm: -------------------------------------------------------------------------------- 1 | ;Check if string contains substring 2 | DATA SEGMENT 3 | STR DB 'AXYBCSDEF$' 4 | SUBSTR DB 'BCS$' 5 | LEN1 DB 0 6 | LEN2 DB 0 7 | MSG1 DB 10,13,'STRING IS : $' 8 | MSG2 DB 10,13,'SUBSTRING IS : $' 9 | MSG3 DB 10,13,'SUBSTRING IS FOUND AT POSITION : $' 10 | POS DB -1 11 | RTN DB '-1$' 12 | DATA ENDS 13 | DISPLAY MACRO MSG 14 | MOV AH,9 15 | LEA DX,MSG 16 | INT 21H 17 | ENDM 18 | CODE SEGMENT 19 | ASSUME CS:CODE,DS:DATA 20 | START: 21 | MOV AX,DATA 22 | MOV DS,AX 23 | DISPLAY MSG1 24 | DISPLAY STR 25 | DISPLAY MSG2 26 | DISPLAY SUBSTR 27 | LEA SI,STR 28 | NXT1: 29 | CMP [SI],'$' 30 | JE DONE1 31 | INC LEN1 32 | INC SI 33 | JMP NXT1 34 | DONE1: 35 | LEA DI,SUBSTR 36 | NXT2: 37 | CMP [DI],'$' 38 | JE DONE2 39 | INC LEN2 40 | INC DI 41 | JMP NXT2 42 | DONE2: 43 | DISPLAY MSG3 44 | LEA SI,STR 45 | MOV AL,LEN1 46 | SUB AL,LEN2 47 | MOV CL,AL 48 | MOV CH,0 49 | FIRST: 50 | INC POS 51 | MOV AL,[SI] 52 | CMP AL,SUBSTR[0] 53 | JE CMPR 54 | INC SI 55 | LOOP FIRST 56 | CMPR: INC SI 57 | MOV AL,[SI] 58 | CMP AL,SUBSTR[1] 59 | JNE NOTEQUAL 60 | INC SI 61 | MOV AL,[SI] 62 | CMP AL,SUBSTR[2] 63 | JE EQUAL 64 | NOTEQUAL: 65 | MOV POS,-1 66 | DISPLAY RTN 67 | JMP EXIT 68 | EQUAL: 69 | MOV DL,POS 70 | ADD DL,30H 71 | MOV AH,2 72 | INT 21H 73 | EXIT: MOV AH,4CH 74 | INT 21H 75 | CODE ENDS 76 | END START 77 | -------------------------------------------------------------------------------- /External Devices/keybrd.asm: -------------------------------------------------------------------------------- 1 | ; this sample shows the use of keyboard functions. 2 | ; try typing something into emulator screen. 3 | ; 4 | ; keyboard buffer is used, when someone types too fast. 5 | ; 6 | ; for realistic emulation, run this example at maximum speed 7 | ; 8 | ; this code will loop until you press esc key, 9 | ; all other keys will be printed. 10 | 11 | name "keybrd" 12 | 13 | org 100h 14 | 15 | ; print a welcome message: 16 | mov dx, offset msg 17 | mov ah, 9 18 | int 21h 19 | 20 | ;============================ 21 | ; eternal loop to get 22 | ; and print keys: 23 | 24 | wait_for_key: 25 | 26 | ; check for keystroke in 27 | ; keyboard buffer: 28 | mov ah, 1 29 | int 16h 30 | jz wait_for_key 31 | 32 | ; get keystroke from keyboard: 33 | ; (remove from the buffer) 34 | mov ah, 0 35 | int 16h 36 | 37 | ; print the key: 38 | mov ah, 0eh 39 | int 10h 40 | 41 | ; press 'esc' to exit: 42 | cmp al, 1bh 43 | jz exit 44 | 45 | jmp wait_for_key 46 | ;============================ 47 | 48 | exit: 49 | ret 50 | 51 | msg db "Type anything...", 0Dh,0Ah 52 | db "[Enter] - carriage return.", 0Dh,0Ah 53 | db "[Ctrl]+[Enter] - line feed.", 0Dh,0Ah 54 | db "You may hear a beep", 0Dh,0Ah 55 | db " when buffer is overflown.", 0Dh,0Ah 56 | db "Press Esc to exit.", 0Dh,0Ah, "$" 57 | 58 | end 59 | -------------------------------------------------------------------------------- /Introduction/9_hello_world_proc.asm: -------------------------------------------------------------------------------- 1 | ;Program to print Hello World using a procedure 2 | 3 | ORG 100H 4 | 5 | LEA SI,msg ;Load effective address of message into source index register 6 | 7 | CALL print_me ;Calls the procedure 8 | 9 | RET 10 | 11 | ;========================================================================================= 12 | 13 | ;this procedure prints a string, the string should end with null 14 | ;only then will the procedure terminate 15 | ;the string address is in SI register to allow for indexing 16 | ;this way the string will be covered as an array of characters 17 | 18 | print_me PROC 19 | 20 | next_char: 21 | CMP b.[SI],0 ;check for zero to stop 22 | JE stop ;short jump if first operand = second operand in CMP 23 | 24 | MOV AL,[SI] ;next get ASCII char 25 | 26 | MOV AH,0EH ;teletype output function number 27 | INT 10H ;video interrupt 28 | 29 | ADD SI,1 ;advance index of string array 30 | 31 | JMP next_char ;go back, and type another char 32 | 33 | stop: 34 | RET ;return to caller 35 | print_me ENDP 36 | 37 | ;========================================================================================= 38 | 39 | msg DB 'Hello World!',0 ;null terminated string 40 | 41 | END -------------------------------------------------------------------------------- /Introduction/13_interrupt_hello_world.asm: -------------------------------------------------------------------------------- 1 | ORG 100H ;instruct compiler to make a simple segment .com file 2 | 3 | ;The sub-function that we are using 4 | ;does not modify the AH register on 5 | ;return, so we may set it only once 6 | 7 | MOV AH,0EH ;select sub-function 8 | 9 | ;INT 10H/0EH sub-function 10 | ;recieves an ASCII code of the 11 | ;character that will be printer 12 | ;in AL register 13 | 14 | MOV AL,'H' ;ASCII code: 72 15 | INT 10H ;print it! 16 | 17 | MOV AL,'e' ;ASCII code: 101 18 | INT 10H ;print it! 19 | 20 | MOV AL,'l' ;ASCII code: 108 21 | INT 10H ;print it! 22 | 23 | MOV AL,'l' ;ASCII code: 108 24 | INT 10H ;print it! 25 | 26 | MOV AL,'o' ;ASCII code: 33 27 | INT 10H ;print it! 28 | 29 | MOV AL,' ' ;ASCII code: 5E 30 | INT 10H ;print it! 31 | 32 | MOV AL,'W' ;ASCII code: 87 33 | INT 10H ;print it! 34 | 35 | MOV AL,'o' ;ASCII code: 111 36 | INT 10H ;print it! 37 | 38 | MOV AL,'r' ;ASCII code: 114 39 | INT 10H ;print it! 40 | 41 | MOV AL,'l' ;ASCII code: 108 42 | INT 10H ;print it! 43 | 44 | MOV AL,'d' ;ASCII code: 100 45 | INT 10H ;print it! 46 | 47 | MOV AL,'!' ;ASCII code: 33 48 | INT 10H ;print it! 49 | 50 | RET ;returns to the operating system -------------------------------------------------------------------------------- /Expression/fibonacci.asm: -------------------------------------------------------------------------------- 1 | ;Code for Program to Print the Fibonacci series in Assembly Language 2 | .MODEL SMALL 3 | 4 | .STACK 64 5 | 6 | .DATA 7 | VAL1 DB 01H 8 | VAL2 DB 01H 9 | LP DB 00H 10 | V1 DB 00H 11 | V2 DB 00H 12 | NL DB 0DH,0AH,'$' 13 | 14 | .CODE 15 | 16 | MAIN PROC 17 | MOV AX,@DATA 18 | MOV DS,AX 19 | 20 | MOV AH,01H 21 | INT 21H 22 | MOV CL,AL 23 | SUB CL,30H 24 | SUB CL,2 25 | 26 | MOV AH,02H 27 | MOV DL,VAL1 28 | ADD DL,30H 29 | INT 21H 30 | 31 | MOV AH,09H 32 | LEA DX,NL 33 | INT 21H 34 | 35 | MOV AH,02H 36 | MOV DL,VAL2 37 | ADD DL,30H 38 | INT 21H 39 | 40 | MOV AH,09H 41 | LEA DX,NL 42 | INT 21H 43 | 44 | 45 | DISP: 46 | MOV BL,VAL1 47 | ADD BL,VAL2 48 | 49 | MOV AH,00H 50 | MOV AL,BL 51 | MOV LP,CL 52 | MOV CL,10 53 | DIV CL 54 | MOV CL,LP 55 | 56 | MOV V1,AL 57 | MOV V2,AH 58 | 59 | MOV DL,V1 60 | ADD DL,30H 61 | MOV AH,02H 62 | INT 21H 63 | 64 | MOV DL,V2 65 | ADD DL,30H 66 | MOV AH,02H 67 | INT 21H 68 | 69 | MOV DL,VAL2 70 | MOV VAL1,DL 71 | MOV VAL2,BL 72 | 73 | MOV AH,09H 74 | LEA DX,NL 75 | INT 21H 76 | 77 | 78 | LOOP DISP 79 | 80 | MOV AH,4CH 81 | INT 21H 82 | 83 | MAIN ENDP 84 | END MAIN 85 | -------------------------------------------------------------------------------- /Conversion/reverse_number.asm: -------------------------------------------------------------------------------- 1 | data segment 2 | num1 dw 12345 3 | num2 dw ? 4 | arry db 10 dup (0) 5 | temp dw ? 6 | msg1 db 10,13,'stored number in memory is : $' 7 | msg2 db 10,13,'reverse number is : $' 8 | res db 10 dup ('$') 9 | data ends 10 | 11 | display macro msg 12 | mov ah,9 13 | lea dx,msg 14 | int 21h 15 | endm 16 | 17 | code segment 18 | assume cs:code,ds:data 19 | start: 20 | mov ax,data 21 | mov ds,ax 22 | display msg1 23 | mov ax,num1 24 | lea si,res 25 | call hex2dec 26 | lea dx,res 27 | mov ah,9 28 | int 21h 29 | lea si,arry 30 | mov ax,num1 31 | reve: 32 | mov dx,0 33 | mov bx,10 34 | div bx 35 | mov arry[si],dl 36 | mov temp,ax 37 | mov ax,dx 38 | inc si 39 | mov ax,temp 40 | cmp temp,0 41 | jg reve 42 | lea di,arry 43 | last: 44 | inc di 45 | cmp arry[di],0 46 | jg last 47 | dec di 48 | mov al,arry[di] 49 | mov ah,0 50 | mov num2,ax 51 | mov cx,10 52 | conv: 53 | dec di 54 | mov al,arry[di] 55 | mov ah,0 56 | mul cx 57 | add num2,ax 58 | mov ax,cx 59 | mov bx,10 60 | mul bx 61 | mov cx,ax 62 | cmp arry[di],0 63 | jg conv 64 | display msg2 65 | mov ax,num2 66 | lea si,res 67 | call hex2dec 68 | lea dx,res 69 | mov ah,9 70 | int 21h 71 | mov ah,4ch 72 | int 21h 73 | code ends 74 | hex2dec proc near 75 | mov cx,0 76 | mov bx,10 77 | loop1: 78 | mov dx,0 79 | div bx 80 | add dl,30h 81 | push dx 82 | inc cx 83 | cmp ax,9 84 | jg loop1 85 | add al,30h 86 | mov [si],al 87 | loop2: 88 | pop ax 89 | inc si 90 | mov [si],al 91 | loop loop2 92 | ret 93 | hex2dec endp 94 | end start -------------------------------------------------------------------------------- /Expression/count_words.asm: -------------------------------------------------------------------------------- 1 | ;TO COUNT THE NUMBER OF WORDS PRESENTED IN THE ENTERED SENTENCE. 2 | 3 | .MODEL SMALL 4 | .STACK 64 5 | .DATA 6 | MAXLEN Db 100 7 | ACTCHAR Db ? 8 | STR DB 101 DUP('$') 9 | STR1 DB "NO. OF WORDS IS ",'$' 10 | .CODE 11 | MAIN PROC FAR 12 | MOV AX, @DATA 13 | MOV DS, AX 14 | MOV CX, 00 15 | MOV BX, 00 16 | MOV AX, 00 17 | LEA DX, MAXLEN 18 | MOV AH, 0AH 19 | INT 21H 20 | 21 | 22 | MOV CH, 00H 23 | MOV Cl, ACTCHAR 24 | MOV DX, 0100H 25 | CMP STR [0], ' ' 26 | JNZ L1 27 | SUB DH, 01 28 | 29 | L1: CMP STR [BX], ' ' 30 | JNZ L3 31 | L2: INC BX 32 | DEC CX 33 | CMP STR [BX], ' ' 34 | JZ l2 35 | INC DH 36 | CMP DH, 0AH 37 | JB L3 38 | MOV DH, 00 39 | INC DL 40 | L3: INC BX 41 | LOOP L1 42 | CMP STR [BX-1], ' ' 43 | JNZ L4 44 | SUB DH, 01 45 | JNC L4 46 | SUB DL, 01 47 | ADD DH, 0AH 48 | 49 | L4: MOV BX, DX 50 | MOV AH, 02H 51 | MOV DL, 0AH 52 | INT 21H 53 | MOV DL, 0DH 54 | INT 21H 55 | LEA DX, STR1 56 | MOV AH, 09H 57 | INT 21H 58 | 59 | MOV DX, BX 60 | ADD DL, 30H 61 | MOV AH, 02H 62 | INT 21H 63 | ADD DH, 30H 64 | MOV DL, DH 65 | MOV AH, 02H 66 | INT 21H 67 | MOV AX, 4C00H 68 | INT 21H 69 | MAIN ENDP 70 | END MAIN 71 | -------------------------------------------------------------------------------- /External Devices/traffic_lights2.asm: -------------------------------------------------------------------------------- 1 | 2 | ; Traffic ligts test 2 for 3 | ; c:\emu8086\devices\Traffic_Lights.exe 4 | 5 | ; This is just an example of how to set the lights, 6 | ; better if you run it in step-by-step mode. 7 | ; DO NOT RUN AT MAXIMUM SPEED, DO NOT USE REAL CARS. 8 | ;) 9 | 10 | 11 | #start=Traffic_Lights.exe# 12 | 13 | name "traffic2" 14 | 15 | yellow_and_green equ 0000_0110b 16 | red equ 0000_0001b 17 | yellow_and_red equ 0000_0011b 18 | green equ 0000_0100b 19 | 20 | all_red equ 0010_0100_1001b 21 | 22 | start: 23 | nop 24 | 25 | 26 | ; 0,1,2 27 | 28 | mov ax, green 29 | out 4, ax 30 | 31 | mov ax, yellow_and_green 32 | out 4, ax 33 | 34 | mov ax,red 35 | out 4, ax 36 | 37 | mov ax, yellow_and_red 38 | out 4, ax 39 | 40 | 41 | ; 3,4,5 42 | 43 | mov ax, green << 3 44 | out 4, ax 45 | 46 | mov ax, yellow_and_green << 3 47 | out 4, ax 48 | 49 | mov ax,red << 3 50 | out 4, ax 51 | 52 | mov ax, yellow_and_red << 3 53 | out 4, ax 54 | 55 | 56 | 57 | ; 6,7,8 58 | 59 | mov ax, green << 6 60 | out 4, ax 61 | 62 | mov ax, yellow_and_green << 6 63 | out 4, ax 64 | 65 | mov ax,red << 6 66 | out 4, ax 67 | 68 | mov ax, yellow_and_red << 6 69 | out 4, ax 70 | 71 | 72 | 73 | ; 9,A,B 74 | 75 | mov ax, green << 9 76 | out 4, ax 77 | 78 | mov ax, yellow_and_green << 9 79 | out 4, ax 80 | 81 | mov ax,red << 9 82 | out 4, ax 83 | 84 | mov ax, yellow_and_red << 9 85 | out 4, ax 86 | 87 | 88 | ; all 89 | 90 | mov ax, all_red 91 | out 4, ax 92 | 93 | mov ax, all_red << 1 ; all yellow 94 | out 4, ax 95 | 96 | mov ax, all_red << 2 ; all green :) 97 | out 4, ax 98 | 99 | 100 | jmp start 101 | -------------------------------------------------------------------------------- /Simulation/water_level_controller.asm: -------------------------------------------------------------------------------- 1 | ;Emulate water level controller on emu8086 with the following specifications: 2 | ;a. No. of water levels in the overhead tank is 8 3 | ;b. Display the current level of water with a buzzer 4 | ;c. Switch on the motor if the water level is 1 5 | ;d. Switch off the motor if the water level is 8 6 | ;e. Switch on the buzzer on water overflow 7 | 8 | DATA SEGMENT 9 | msg1 db 10,13,"The water level is: $" 10 | msg2 db 10,13,"Switch ON motor. $" 11 | msg3 db 10,13,"Switch OFF motor. $" 12 | msg4 db 10,13,"Water overflow! $" 13 | DATA ENDS 14 | 15 | CODE SEGMENT 16 | ASSUME DS:DATA,CS:CODE 17 | 18 | START: mov AX,@data ;intialize data segment 19 | mov DS,AX 20 | 21 | mov CL,1H 22 | 23 | L1: lea DX,msg1 ;displaying water level message 24 | mov AH,9H 25 | int 21H 26 | 27 | add CL,30H ;ASCII adjust before displaying 28 | mov DL,CL 29 | mov AH,2H ;display 30 | int 21H 31 | sub CL,30H ;ASCII adjust after displaying 32 | 33 | cmp CL,8H ;switch off motor 34 | je off ;jump to off if = 8 35 | 36 | cmp CL,1H ;switch on motor 37 | je on ;jump to on if = 1 38 | 39 | back: inc CL ;increase water level by 1 40 | cmp CL,8H ;check if water level is overflowing 41 | jle l1 42 | 43 | jmp exit 44 | 45 | on: lea DX,msg2 46 | mov AH,9h 47 | int 21H 48 | jmp back 49 | 50 | off: lea DX,msg3 51 | mov AH,9h 52 | int 21H 53 | lea DX,msg4 ;displaying overflow 54 | mov AH,9H 55 | int 21H 56 | jmp back 57 | 58 | 59 | exit: mov AH,4CH 60 | int 21H 61 | 62 | CODE ENDS 63 | END START -------------------------------------------------------------------------------- /External Devices/stepper_motor.asm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ; this is an example of out instruction. 5 | ; it writes values to virtual i/o port 6 | ; that controls the stepper-motor. 7 | ; c:\emu8086\devices\stepper_motor.exe is on port 7 8 | 9 | #start=stepper_motor.exe# 10 | 11 | 12 | name "stepper" 13 | 14 | #make_bin# 15 | 16 | steps_before_direction_change = 20h ; 32 (decimal) 17 | 18 | jmp start 19 | 20 | ; ========= data =============== 21 | 22 | ; bin data for clock-wise 23 | ; half-step rotation: 24 | datcw db 0000_0110b 25 | db 0000_0100b 26 | db 0000_0011b 27 | db 0000_0010b 28 | 29 | ; bin data for counter-clock-wise 30 | ; half-step rotation: 31 | datccw db 0000_0011b 32 | db 0000_0001b 33 | db 0000_0110b 34 | db 0000_0010b 35 | 36 | 37 | ; bin data for clock-wise 38 | ; full-step rotation: 39 | datcw_fs db 0000_0001b 40 | db 0000_0011b 41 | db 0000_0110b 42 | db 0000_0000b 43 | 44 | ; bin data for counter-clock-wise 45 | ; full-step rotation: 46 | datccw_fs db 0000_0100b 47 | db 0000_0110b 48 | db 0000_0011b 49 | db 0000_0000b 50 | 51 | 52 | start: 53 | mov bx, offset datcw ; start from clock-wise half-step. 54 | mov si, 0 55 | mov cx, 0 ; step counter 56 | 57 | next_step: 58 | ; motor sets top bit when it's ready to accept new command 59 | wait: in al, 7 60 | test al, 10000000b 61 | jz wait 62 | 63 | mov al, [bx][si] 64 | out 7, al 65 | 66 | inc si 67 | 68 | cmp si, 4 69 | jb next_step 70 | mov si, 0 71 | 72 | inc cx 73 | cmp cx, steps_before_direction_change 74 | jb next_step 75 | 76 | mov cx, 0 77 | add bx, 4 ; next bin data 78 | 79 | cmp bx, offset datccw_fs 80 | jbe next_step 81 | 82 | mov bx, offset datcw ; return to clock-wise half-step. 83 | 84 | jmp next_step 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /Introduction/14_display_time.asm: -------------------------------------------------------------------------------- 1 | DATA SEGMENT 2 | 3 | MSG1 DB 'Current time is: $' 4 | HR DB ? 5 | MIN DB ? 6 | SEC DB ? 7 | MSEC DB ? 8 | 9 | DATA ENDS 10 | 11 | CODE SEGMENT 12 | 13 | ASSUME CS:CODE, DS:DATA 14 | START: MOV AX, DATA 15 | MOV DS, AX 16 | 17 | MOV AH,2CH ; To get system time 18 | INT 21H 19 | 20 | MOV HR, CH ; CH -> Hours 21 | MOV MIN, CL ; CL -> Minutes 22 | MOV SEC, DH ; DH -> Seconds 23 | MOV MSEC, DL ; DL -> 1/100th second 24 | 25 | LEA DX, MSG1 ; Display MSG1 26 | MOV AH, 09H 27 | INT 21H 28 | 29 | MOV AL, HR ; If AL=0D AAM will split the nibbles into AH and AL 30 | AAM ; So AH=01 and AL=03 31 | MOV BX, AX 32 | CALL DISPLAY ; Display hours 33 | MOV DL, ':' ; Display ':' after displaying hours 34 | MOV AH, 02H 35 | INT 21H 36 | 37 | MOV AL, MIN 38 | AAM 39 | MOV BX, AX 40 | CALL DISPLAY ; Display minutes 41 | MOV DL, ':' ; Display ':' after displaying minutes 42 | MOV AH, 02H 43 | INT 21H 44 | 45 | MOV AL, SEC 46 | AAM 47 | MOV BX, AX 48 | CALL DISPLAY ; Display seconds 49 | MOV DL, '.' ; Display '.' after displaying seconds 50 | MOV AH, 02H 51 | INT 21H 52 | 53 | MOV AL, MSEC 54 | AAM 55 | MOV BX, AX 56 | CALL DISPLAY ; Display 1/100th seconds 57 | MOV AH, 4CH 58 | INT 21H 59 | 60 | DISPLAY PROC NEAR ; Procedure that displays time 61 | MOV DL, BH 62 | ADD DL, 30H ; Display BH value 63 | MOV AH, 02H 64 | INT 21H 65 | MOV DL, BL 66 | ADD DL, 30H ; Display BL value 67 | MOV AH, 02H 68 | INT 21H 69 | RET 70 | DISPLAY ENDP 71 | 72 | CODE ENDS 73 | 74 | END START -------------------------------------------------------------------------------- /Expression/given_number_prime.asm: -------------------------------------------------------------------------------- 1 | ;# 8086 Program to check the given no for Prime # 2 | 3 | ;NUM is assigned to the Number to be checked 4 | ;AL is used as the default no 5 | ;BL is used as Dividing Variable. It is Incremented in each loop 6 | ;BH is used to know the number of variables which can divide the Number. BH>02, The number is not prime 7 | ;BL is made to run x number of times, where x is the given number. 8 | 9 | ; Declaration Part 10 | .MODEL SMALL 11 | .DATA 12 | MSG DB "The Give No is a Prime No$" 13 | NMSG DB "The Given No is not a Prime No$" 14 | NUM DB 71H ;Enter the required no here 15 | .CODE 16 | 17 | START: MOV AX,@DATA 18 | MOV DS,AX 19 | 20 | MOV AL,NUM 21 | MOV BL,02H ; The Dividing starts from 2, Hence BH is compare to 02H 22 | MOV DX,0000H ; To avoid Divide overflow error 23 | MOV AH,00H ; To avoid Divide overflow error 24 | 25 | ;Loop to check for Prime No 26 | L1:DIV BL 27 | CMP AH,00H ; Remainder is compared with 00H (AH) 28 | JNE NEXT 29 | INC BH ; BH is incremented if the Number is divisible by current value of BL 30 | NEXT:CMP BH,02H ; If BH > 02H, There is no need to proceed, It is not a Prime 31 | JE FALSE ; The no is not a Prime No 32 | INC BL ; Increment BL 33 | MOV AX,0000H ; To avoid Divide overflow error 34 | MOV DX,0000H ; To avoid Divide overflow error 35 | MOV AL,NUM ; Move the Default no to AL 36 | CMP BL,NUM ; Run the loop until BL matches Number. I.e, Run loop x no of times, where x is the Number given 37 | JNE L1 ; Jump to check again with incremented value of BL 38 | 39 | ;To display The given no is a Prime No 40 | TRUE: LEA DX,MSG 41 | MOV AH,09H ; Used to print a string 42 | INT 21H 43 | JMP EXIT 44 | 45 | ;To display The given no is not a Prime No 46 | FALSE: LEA DX,NMSG 47 | MOV AH,09H ; Used to print a string 48 | INT 21H 49 | 50 | 51 | EXIT: 52 | MOV AH,4CH 53 | INT 21H 54 | END START 55 | -------------------------------------------------------------------------------- /Arithmetic/multiply_2_32b.asm: -------------------------------------------------------------------------------- 1 | .data ; data segment starts 2 | a dw 5678h, 1234h, 5 dup(0) ;a is 32bit number a=1234 5678 3 | b dw 1111h, 1111h, 5 dup(0) ;b is 32bit number b=1111 1111 4 | c dw 4 dup(?) ;reserve 4 words 5 | 6 | .code 7 | start: 8 | mov ax,@data 9 | mov ds,ax 10 | lea si,a 11 | lea bx,b 12 | lea di,c ;point to first word 13 | 14 | mov ax,word ptr [si] ;take lower 16bits (5678) of a into ax 15 | mul word ptr [bx+0] ;multiply ax with lower 16bits of b(1111) and store in ax 16 | mov [di],ax ;move the contents of ax to c[di] 17 | mov cx,dx ;move the value of dx to cx 18 | 19 | mov ax,word ptr [si+2] ;take higher 16bits (1234) of a into ax 20 | mul word ptr [bx+0] ;multiply ax with lower 16bits of b(1111)and store in ax 21 | add cx,ax ;cx=cx+ax 22 | mov [di+2],cx ;move the contents of cx to c[di+2] 23 | mov cx,dx ;move contents of dx to cx 24 | 25 | mov ax,word ptr [si] ;take lower 16bits(5678) of a in ax 26 | mul word ptr [bx+2] ;multiply contents of ax with higher 16bits of b(1111) 27 | add word ptr [di+2],ax ;c[di+2]=c[di+2]+ax 28 | adc cx,dx ;cx=cx+dx+cf 29 | mov [di+4],ax ;move contents of ax to c[di+4] 30 | 31 | mov ax,word ptr [si+2] ;take higher 16bits of a(1234) into ax 32 | mul word ptr [bx+2] ;multiply ax with higher 16bits of b(1111) and store in ax 33 | add cx,ax ;cx=cx+ax 34 | mov word ptr [di+4],cx ;move contents of cx to c[di+4] 35 | adc dx,0000 ;dx=dx+0000+cf 36 | mov [di+6],dx ;move the contents of dx to c[di+6] 37 | 38 | int 03h ;halt 39 | 40 | end start -------------------------------------------------------------------------------- /Conversion/decimal_to_octal.asm: -------------------------------------------------------------------------------- 1 | ;Code for Program to Convert Decimal number to Octal number in Assembly Language 2 | prnstr macro msg 3 | mov ah, 09h 4 | lea dx, msg 5 | int 21h 6 | endm 7 | 8 | data segment 9 | buf1 db "Enter a decimal number : $" 10 | buf2 db 0ah, "Invalid Decimal Number...$" 11 | buf3 db 0ah, "Equivalent octal number is : $" 12 | buf4 db 6 13 | db 0 14 | db 6 dup(0) 15 | multiplier db 0ah 16 | data ends 17 | 18 | code segment 19 | assume cs:code, ds:data 20 | start : 21 | mov ax, data 22 | mov ds, ax 23 | mov es, ax 24 | 25 | prnstr buf1 26 | 27 | mov ah, 0ah 28 | lea dx, buf4 29 | int 21h 30 | 31 | mov si, offset buf4 + 2 32 | mov cl, byte ptr [si-1] 33 | mov ch, 00h 34 | subtract : 35 | mov al, byte ptr [si] 36 | cmp al, 30h 37 | jnb cont1 38 | prnstr buf2 39 | jmp stop 40 | cont1 : 41 | cmp al, 3ah 42 | jb cont2 43 | prnstr buf2 44 | jmp stop 45 | cont2 : 46 | sub al, 30h 47 | mov byte ptr [si], al 48 | 49 | inc si 50 | loop subtract 51 | 52 | mov si, offset buf4 + 2 53 | mov cl, byte ptr [si-1] 54 | mov ch, 00h 55 | mov ax, 0000h 56 | calc : 57 | mul multiplier 58 | mov bl, byte ptr [si] 59 | mov bh, 00h 60 | add ax, bx 61 | inc si 62 | loop calc 63 | 64 | mov si, offset buf4 + 2 65 | mov bx, ax 66 | mov dx, 0000h 67 | mov ax, 8000h 68 | convert : 69 | mov cx, 0000h 70 | conv : 71 | cmp bx, ax 72 | jb cont3 73 | sub bx, ax 74 | inc cx 75 | jmp conv 76 | cont3 : 77 | add cl, 30h 78 | mov byte ptr [si], cl 79 | inc si 80 | mov cx, 0008h 81 | div cx 82 | cmp ax, 0000h 83 | jnz convert 84 | 85 | mov byte ptr [si], '$' 86 | prnstr buf3 87 | prnstr buf4+2 88 | stop : 89 | mov ax, 4c00h 90 | int 21h 91 | code ends 92 | end star 93 | -------------------------------------------------------------------------------- /Arithmetic/add_8b_2.asm: -------------------------------------------------------------------------------- 1 | ;program to add two 8b numbers 2 | data segment 3 | num1 db ? 4 | num2 db ? 5 | res db ? 6 | msg1 db 10,13,"enter the first number: $" 7 | msg2 db 10,13,"enter the second number: $" 8 | msg3 db 10,13,"result of addition is: $" 9 | data ends 10 | 11 | assume cs:code,ds:data 12 | 13 | code segment 14 | start: mov ax,data 15 | mov ds,ax ;initialize data segment 16 | 17 | lea dx,msg1 ;load address of msg1 into dx 18 | mov ah,9h ;interrupt to display contents of dx 19 | int 21h 20 | 21 | mov ah,1h ;read a character from console 22 | int 21h 23 | sub al,30h ;convert number into bcd from as 24 | cii form 25 | mov num1,al ;store number as num1 26 | 27 | lea dx,msg2 ;load address of msg2 into dx 28 | mov ah,9h ;interrupt to display contents of dx 29 | int 21h 30 | 31 | mov ah,1h ;read a character from console 32 | int 21h 33 | sub al,30h ;convert number into bcd from ascii form 34 | mov num2,al ;store number as num2 35 | 36 | add al,num1 ;add num1 to num2 37 | mov res,al ;store sum in res 38 | mov ah,0 ;clear garabage value (ah to be used later) 39 | aaa ;converts hex to bcd and stores values in ah and al 40 | add ah,30h ;first digit converted into bcd 41 | add al,30h ;second digit converted from ascii to bcd 42 | 43 | mov bx,ax ;save value of ax into bx 44 | lea dx,msg3 ;print ms3 45 | mov ah,9h 46 | int 21h 47 | 48 | mov ah,2h ;print first digit 49 | mov dl,bh 50 | int 21h 51 | 52 | mov ah,2 ;print second digit 53 | mov dl,bl 54 | int 21h 55 | 56 | mov ah,4ch 57 | int 21h 58 | 59 | code ends 60 | end start -------------------------------------------------------------------------------- /Arithmetic/sub_8b.asm: -------------------------------------------------------------------------------- 1 | ;program to subtract two 8b numbers 2 | data segment 3 | num1 db ? 4 | num2 db ? 5 | res db ? 6 | msg1 db 10,13,"enter the first number: $" 7 | msg2 db 10,13,"enter the second number: $" 8 | msg3 db 10,13,"result of addition is: $" 9 | data ends 10 | 11 | assume cs:code,ds:data 12 | 13 | code segment 14 | start: mov ax,data 15 | mov ds,ax ;initialize data segment 16 | 17 | lea dx,msg1 ;load address of msg1 into dx 18 | mov ah,9h ;interrupt to display contents of dx 19 | int 21h 20 | 21 | mov ah,1h ;read a character from console 22 | int 21h 23 | sub al,30h ;convert number into bcd from ascii form 24 | mov num1,al ;store number as num1 25 | 26 | lea dx,msg2 ;load address of msg2 into dx 27 | mov ah,9h ;interrupt to display contents of dx 28 | int 21h 29 | 30 | mov ah,1h ;read a character from console 31 | int 21h 32 | sub al,30h ;convert number into bcd from ascii form 33 | mov num2,al ;store number as num2 34 | 35 | mov al,num1 36 | sub al,num2 ;sub num2 from num1 37 | mov res,al ;store sum in res 38 | mov ah,0 ;clear garabage value (ah to be used later) 39 | aaa ;converts hex to bcd and stores values in ah and al 40 | add ah,30h ;first digit converted into bcd 41 | add al,30h ;second digit converted from ascii to bcd 42 | 43 | mov bx,ax ;save value of ax into bx 44 | lea dx,msg3 ;print ms3 45 | mov ah,9h 46 | int 21h 47 | 48 | mov ah,2h ;print first digit 49 | mov dl,bh 50 | int 21h 51 | 52 | mov ah,2 ;print second digit 53 | mov dl,bl 54 | int 21h 55 | 56 | mov ah,4ch 57 | int 21h 58 | 59 | code ends 60 | end start -------------------------------------------------------------------------------- /Expression/finding_largest_number_memory.asm: -------------------------------------------------------------------------------- 1 | ;find out largest number from an unordered array of sixteen 8-bit 2 | ;numbers stored sequentially in memory location 2000:5000H. Store 3 | ;the largest number in memory location 2000:5000H 4 | 5 | data segment 6 | A DB 13h,2ch,63h,58h,50h,10h,17h,89h,91h,00h,02h,53h,68h,18h,47h,18h; 7 | data ends 8 | 9 | code segment 10 | assume cs:code, ds:data 11 | 12 | start: mov ax,data 13 | mov ds,ax 14 | 15 | lea bx,A ;si points to first number 16 | mov di,5000H ;starting offset address = 5000H 17 | mov cx,0010H ;16 bytes of data 18 | mov dx,0000H ;dx initialized to store numbers 19 | 20 | back: mov dl,byte ptr[bx] ;transfer string to al 21 | 22 | mov ax,2000H ;change segment 23 | mov ds,ax 24 | 25 | mov [di],dl ;transfer string to di 26 | 27 | mov ax,data ;change segment 28 | mov ds,ax 29 | 30 | inc bx ;point to next byte 31 | inc di ;point to next memory address 32 | loop back ;decrement cx by 1 33 | 34 | mov ax,2000H ;change segment 35 | mov ds,ax 36 | 37 | ;--------------clearing registers------------------------------- 38 | 39 | xor cx,cx 40 | xor bx,bx 41 | xor si,si 42 | 43 | ;--------------find largest------------------------------------- 44 | 45 | mov cx, 10h ;set up loop counter 46 | 47 | mov bl, 00h ;bl stores highest number 48 | mov si, 5000H ;si points to first number 49 | 50 | up: mov al,byte ptr[SI] ;compare next number to highest 51 | cmp al, bl 52 | jl nxt ;jump if al is still the highest 53 | mov bl, al ;else bl holds new highest 54 | 55 | nxt: inc si ;point to next number 56 | dec cx 57 | jnz up 58 | 59 | mov dl,bl 60 | 61 | mov [si+1],bl ;store highest number 62 | hlt 63 | 64 | code ends 65 | end start -------------------------------------------------------------------------------- /Expression/concatenation_string.asm: -------------------------------------------------------------------------------- 1 | ;*************************************************** 2 | ; 3 | ; Concatenation of strings in 8086 ALP 4 | 5 | ;macro for printing a string 6 | print macro m 7 | mov ah,09h 8 | mov dx,offset m 9 | int 21h 10 | endm 11 | 12 | .model small 13 | 14 | 15 | ;****** Data Segment ****** 16 | .data 17 | 18 | empty db 10,13, " $" 19 | str1 db 25,?,25 dup('$') 20 | str2 db 25,?,25 dup('$') 21 | 22 | mstring db 10,13, "Enter the string: $" 23 | mstring2 db 10,13, "Enter second string: $" 24 | mconcat db 10,13, "Concatenated string: $" 25 | 26 | ;********** Code Segment ************ 27 | 28 | .code 29 | 30 | start: 31 | mov ax,@data 32 | mov ds,ax 33 | 34 | print mstring 35 | call accept_string 36 | 37 | ;storing string in str2 38 | print mstring2 39 | mov ah,0ah 40 | lea dx,str2 41 | int 21h 42 | 43 | 44 | mov cl,str1+1 ;length of string1 in cl 45 | mov si,offset str1 46 | next: inc si 47 | dec cl 48 | jnz next 49 | inc si 50 | 51 | inc si 52 | mov di,offset str2 53 | inc di 54 | inc di 55 | 56 | mov cl,str2+1 57 | move_next: 58 | 59 | mov al,[di] 60 | mov [si],al 61 | inc si 62 | inc di 63 | dec cl 64 | jnz move_next 65 | 66 | print mconcat 67 | print str1+2 68 | 69 | 70 | exit: 71 | mov ah,4ch ;exit the program 72 | int 21h 73 | 74 | 75 | ;accept procedure 76 | 77 | accept proc near 78 | 79 | mov ah,01 80 | int 21h 81 | ret 82 | accept endp 83 | 84 | display1 proc near 85 | 86 | mov al,bl 87 | mov bl,al 88 | and al,0f0h 89 | mov cl,04 90 | rol al,cl 91 | 92 | cmp al,09 93 | jbe number 94 | add al,07 95 | number: add al,30h 96 | mov dl,al 97 | mov ah,02 98 | int 21h 99 | 100 | mov al,bl 101 | and al,00fh 102 | cmp al,09 103 | jbe number2 104 | add al,07 105 | number2: add al,30h 106 | mov dl,al 107 | mov ah,02 108 | int 21h 109 | ret 110 | display1 endp 111 | 112 | 113 | 114 | accept_string proc near 115 | 116 | mov ah,0ah ;accept string from user function 117 | mov dx,offset str1 ; store the string in memory pointed by "DX" 118 | int 21h 119 | ret 120 | accept_string endp 121 | 122 | end start 123 | end 124 | 125 | -------------------------------------------------------------------------------- /Simulation/fire_monitoring_system.asm: -------------------------------------------------------------------------------- 1 | ;Emulate a fire monitoring system on emu8086 for the following specifications: 2 | ;Define the threshold for the temperature of two rooms 3 | ;Generate the temperature value in 8b resolution 4 | ;Switch on the alarm and display an alarm message when the threshold of either of the room is reached 5 | ;Remove the alarm and bring the temperature below the threshold 6 | 7 | DATA SEGMENT 8 | num1 db ? 9 | num2 db ? 10 | MSG1 DB 10,13,"Threshold for 1: $" 11 | MSG2 DB 10,13,"Threshold for 2: $" 12 | MSG3 DB 10,13,"Alarm on! $" 13 | MSG4 DB 10,13,"Threshold reached! $" 14 | MSG5 DB 10,13,"Press '1' to restart. $" 15 | MSG6 DB 10,13,"Temp: $" 16 | DATA ENDS 17 | 18 | CODE SEGMENT 19 | ASSUME DS:DATA,CS:CODE 20 | 21 | START: mov AX,data ;intialize data segment 22 | mov DS,AX 23 | 24 | lea dx,msg1 ;load and display message 1 25 | mov ah,9h 26 | int 21h 27 | 28 | mov ah,1h ;read character from console 29 | int 21h 30 | sub al,30h ;convert from ASCII to BCD 31 | mov num1,al ;store number as num1 32 | 33 | lea dx,msg2 ;load and display message 2 34 | mov ah,9h 35 | int 21h 36 | 37 | mov ah,1h ;read character from console 38 | int 21h 39 | sub al,30h ;convert from ASCII to BCD 40 | mov num2,al ;store number as num2 41 | 42 | l0: mov cl,0h ;initial temperature at 0 43 | 44 | l1: lea dx,msg6 ;load and display message 6 45 | mov ah,9h 46 | int 21h 47 | 48 | add cl,30h ;ASCII adjust before displaying 49 | mov dl,cl 50 | mov ah,2h ;display it 51 | int 21h 52 | sub cl,30h ;ASCII adjust after display 53 | 54 | inc cl ;temperature of the room increases 55 | cmp cl,num1 ;check against first threshold 56 | jge re 57 | 58 | cmp cl,num2 ;check against second threshold 59 | jge re 60 | 61 | jmp l1 ;continue increasing 62 | 63 | re: lea dx,msg3 ;load and display alarm is on 64 | mov ah,9h 65 | int 21h 66 | 67 | lea dx,msg4 ;load and display threshold 68 | mov ah,9h 69 | int 21h 70 | 71 | lea dx,msg5 ;load and display restart 72 | mov ah,9h 73 | int 21h 74 | 75 | mov ah,1h 76 | int 21h 77 | sub al,30h 78 | 79 | cmp al,1h 80 | je l0 81 | 82 | exit: mov ah,4ch 83 | int 21h -------------------------------------------------------------------------------- /Simulation/garment_defect.asm: -------------------------------------------------------------------------------- 1 | ;Design and Emulate a smart automation system for a garment manufacturing unit with 2 | ;the following requirements:- 3 | ; To detect all possible defects. 4 | ; To remove the defective pieces. 5 | ; To provide comprehensive inventory report. 6 | 7 | data segment 8 | num1 db ? 9 | num2 db ? 10 | line DB 1h,2h,3h,4h,5h,6h,7h,8h,9h,0h; 11 | MSG1 DB 10,13,"Threshold 1: $" 12 | MSG2 DB 10,13,"Threshold 2: $" 13 | MSG3 DB 10,13,"Defect detected $" 14 | MSG4 DB 10,13,"Defect removed $" 15 | MSG5 DB 10,13,"Good clothes: $" 16 | MSG6 DB 10,13,"Bad clothes: $" 17 | good db ? 18 | bad db ? 19 | data ends 20 | 21 | code segment 22 | assume cs:code, ds:data 23 | start: mov ax, data 24 | mov ds, ax 25 | mov cx, 0bh ;set up loop counter 26 | 27 | mov good,0Ah 28 | mov bad,0000H 29 | 30 | lea dx,msg1 ;load and display message 1 31 | mov ah,9h 32 | int 21h 33 | 34 | mov ah,1h ;read character from console 35 | int 21h 36 | sub al,30h ;convert from ASCII to BCD 37 | mov num1,al ;store number as num1 38 | 39 | lea dx,msg2 ;load and display message 2 40 | mov ah,9h 41 | int 21h 42 | 43 | mov ah,1h ;read character from console 44 | int 21h 45 | sub al,30h ;convert from ASCII to BCD 46 | mov num2,al ;store number as num2 47 | 48 | up: mov al,byte ptr[SI] ;compare next 49 | cmp al,num1 50 | jl dft 51 | cmp al,num2 52 | jl dft 53 | jmp nxt 54 | 55 | dft: lea dx,msg3 ;load and display message 3 56 | mov ah,9h 57 | int 21h 58 | 59 | mov [si],0000H ;remove defect 60 | inc bad 61 | 62 | lea dx,msg4 ;load and display message 4 63 | mov ah,9h 64 | int 21h 65 | 66 | jmp nxt 67 | 68 | nxt: inc si ;point to next grade 69 | dec cx 70 | jnz up 71 | 72 | lea dx,msg5 ;load and display message 5 73 | mov ah,9h 74 | int 21h 75 | 76 | xor ax,ax 77 | mov al,bad 78 | sub good,al 79 | add good,30h ;ASCII adjust before displaying 80 | mov dl,good 81 | mov ah,2h ;display it 82 | int 21h 83 | 84 | lea dx,msg6 ;load and display message 6 85 | mov ah,9h 86 | int 21h 87 | 88 | add bad,30h ;ASCII adjust before displaying 89 | mov dl,bad 90 | mov ah,2h ;display it 91 | int 21h 92 | 93 | hlt 94 | 95 | code ends 96 | end start -------------------------------------------------------------------------------- /External Devices/mouse.asm: -------------------------------------------------------------------------------- 1 | ; mouse test 2 | 3 | name "mouse" 4 | 5 | org 100h 6 | 7 | print macro x, y, attrib, sdat 8 | LOCAL s_dcl, skip_dcl, s_dcl_end 9 | pusha 10 | mov dx, cs 11 | mov es, dx 12 | mov ah, 13h 13 | mov al, 1 14 | mov bh, 0 15 | mov bl, attrib 16 | mov cx, offset s_dcl_end - offset s_dcl 17 | mov dl, x 18 | mov dh, y 19 | mov bp, offset s_dcl 20 | int 10h 21 | popa 22 | jmp skip_dcl 23 | s_dcl DB sdat 24 | s_dcl_end DB 0 25 | skip_dcl: 26 | endm 27 | 28 | clear_screen macro 29 | pusha 30 | mov ax, 0600h 31 | mov bh, 0000_1111b 32 | mov cx, 0 33 | mov dh, 24 34 | mov dl, 79 35 | int 10h 36 | popa 37 | endm 38 | 39 | print_space macro num 40 | pusha 41 | mov ah, 9 42 | mov al, ' ' 43 | mov bl, 0000_1111b 44 | mov cx, num 45 | int 10h 46 | popa 47 | endm 48 | 49 | 50 | jmp start 51 | 52 | curX dw 0 53 | curY dw 0 54 | curB dw 0 55 | 56 | 57 | start: 58 | mov ax, 1003h ; disable blinking. 59 | mov bx, 0 60 | int 10h 61 | 62 | ; hide text cursor: 63 | mov ch, 32 64 | mov ah, 1 65 | int 10h 66 | 67 | 68 | ; reset mouse and get its status: 69 | mov ax, 0 70 | int 33h 71 | cmp ax, 0 72 | jne ok 73 | print 1,1,0010_1111b, " mouse not found :-( " 74 | jmp stop 75 | 76 | ok: 77 | clear_screen 78 | 79 | print 7,7,0010_1011b," note: in the emulator you may need to press and hold mouse buttons " 80 | print 7,8,0010_1011b," because mouse interrupts are not processed in real time. " 81 | print 7,9,0010_1011b," for a real test, click external->run from the menu. " 82 | print 10,11,0010_1111b," click/hold both buttons to exit... " 83 | 84 | ; display mouse cursor: 85 | mov ax, 1 86 | int 33h 87 | 88 | check_mouse_buttons: 89 | mov ax, 3 90 | int 33h 91 | cmp bx, 3 ; both buttons 92 | je hide 93 | cmp cx, curX 94 | jne print_xy 95 | cmp dx, curY 96 | jne print_xy 97 | cmp bx, curB 98 | jne print_buttons 99 | 100 | 101 | print_xy: 102 | print 0,0,0000_1111b,"x=" 103 | mov ax, cx 104 | call print_ax 105 | print_space 4 106 | print 0,1,0000_1111b,"y=" 107 | mov ax, dx 108 | call print_ax 109 | print_space 4 110 | mov curX, cx 111 | mov curY, dx 112 | jmp check_mouse_buttons 113 | 114 | print_buttons: 115 | print 0,2,0000_1111b,"btn=" 116 | mov ax, bx 117 | call print_ax 118 | print_space 4 119 | mov curB, bx 120 | jmp check_mouse_buttons 121 | 122 | 123 | 124 | hide: 125 | mov ax, 2 ; hide mouse cursor. 126 | int 33h 127 | 128 | clear_screen 129 | 130 | print 1,1,1010_0000b," hardware must be free! free the mice! " 131 | 132 | stop: 133 | ; show box-shaped blinking text cursor: 134 | mov ah, 1 135 | mov ch, 0 136 | mov cl, 8 137 | int 10h 138 | 139 | print 4,7,0000_1010b," press any key.... " 140 | mov ah, 0 141 | int 16h 142 | 143 | ret 144 | 145 | 146 | print_ax proc 147 | cmp ax, 0 148 | jne print_ax_r 149 | push ax 150 | mov al, '0' 151 | mov ah, 0eh 152 | int 10h 153 | pop ax 154 | ret 155 | print_ax_r: 156 | pusha 157 | mov dx, 0 158 | cmp ax, 0 159 | je pn_done 160 | mov bx, 10 161 | div bx 162 | call print_ax_r 163 | mov ax, dx 164 | add al, 30h 165 | mov ah, 0eh 166 | int 10h 167 | jmp pn_done 168 | pn_done: 169 | popa 170 | ret 171 | endp 172 | -------------------------------------------------------------------------------- /External Devices/robot.asm: -------------------------------------------------------------------------------- 1 | 2 | #start=robot.exe# 3 | 4 | name "robot" 5 | 6 | #make_bin# 7 | #cs = 500# 8 | #ds = 500# 9 | #ss = 500# ; stack 10 | #sp = ffff# 11 | #ip = 0# 12 | 13 | ; this is an example of contoling the robot. 14 | 15 | ; this code randomly moves the robot, 16 | ; and makes it to switch the lamps on and off. 17 | 18 | ; robot is a mechanical creature and it takes 19 | ; some time for it to complete a task. 20 | ; status register is used to see if robot is busy or not. 21 | 22 | ; c:\emu8086\devices\robot.exe uses ports 9, 10 and 11 23 | ; source code of the robot and other devices is in: 24 | ; c:\emu8086\devices\developer\sources\ 25 | ; robot is programmed in visual basic 6.0 26 | 27 | 28 | ; robot base i/o port: 29 | r_port equ 9 30 | 31 | ;=================================== 32 | 33 | eternal_loop: 34 | ; wait until robot 35 | ; is ready: 36 | call wait_robot 37 | 38 | ; examine the area 39 | ; in front of the robot: 40 | mov al, 4 41 | out r_port, al 42 | 43 | call wait_exam 44 | 45 | ; get result from 46 | ; data register: 47 | in al, r_port + 1 48 | 49 | ; nothing found? 50 | cmp al, 0 51 | je cont ; - yes, so continue. 52 | 53 | ; wall? 54 | cmp al, 255 55 | je cont ; - yes, so continue. 56 | 57 | ; switched-on lamp? 58 | cmp al, 7 59 | jne lamp_off ; - no, so skip. 60 | ; - yes, so switch it off, 61 | ; and turn: 62 | call switch_off_lamp 63 | jmp cont ; continue 64 | 65 | lamp_off: nop 66 | 67 | ; if gets here, then we have 68 | ; switched-off lamp, because 69 | ; all other situations checked 70 | ; already: 71 | call switch_on_lamp 72 | 73 | cont: 74 | call random_turn 75 | 76 | call wait_robot 77 | 78 | ; try to step forward: 79 | mov al, 1 80 | out r_port, al 81 | 82 | call wait_robot 83 | 84 | ; try to step forward again: 85 | mov al, 1 86 | out r_port, al 87 | 88 | jmp eternal_loop ; go again! 89 | 90 | ;=================================== 91 | 92 | ; this procedure does not 93 | ; return until robot is ready 94 | ; to receive next command: 95 | wait_robot proc 96 | ; check if robot busy: 97 | busy: in al, r_port+2 98 | test al, 00000010b 99 | jnz busy ; busy, so wait. 100 | ret 101 | wait_robot endp 102 | 103 | ;=================================== 104 | 105 | ; this procedure does not 106 | ; return until robot completes 107 | ; the examination: 108 | wait_exam proc 109 | ; check if has new data: 110 | busy2: in al, r_port+2 111 | test al, 00000001b 112 | jz busy2 ; no new data, so wait. 113 | ret 114 | wait_exam endp 115 | 116 | ;=================================== 117 | 118 | ; switch off the lamp: 119 | switch_off_lamp proc 120 | mov al, 6 121 | out r_port, al 122 | ret 123 | switch_off_lamp endp 124 | 125 | ;=================================== 126 | 127 | ; switch on the lamp: 128 | switch_on_lamp proc 129 | mov al, 5 130 | out r_port, al 131 | ret 132 | switch_on_lamp endp 133 | 134 | ;=================================== 135 | 136 | ; generates a random turn using 137 | ; system timer: 138 | random_turn proc 139 | 140 | ; get number of clock 141 | ; ticks since midnight 142 | ; in cx:dx 143 | mov ah, 0 144 | int 1ah 145 | 146 | ; randomize using xor: 147 | xor dh, dl 148 | xor ch, cl 149 | xor ch, dh 150 | 151 | test ch, 2 152 | jz no_turn 153 | 154 | test ch, 1 155 | jnz turn_right 156 | 157 | ; turn left: 158 | mov al, 2 159 | out r_port, al 160 | ; exit from procedure: 161 | ret 162 | 163 | turn_right: 164 | mov al, 3 165 | out r_port, al 166 | 167 | no_turn: 168 | ret 169 | random_turn endp 170 | 171 | ;=================================== 172 | -------------------------------------------------------------------------------- /External Devices/timer.asm: -------------------------------------------------------------------------------- 1 | ; this sample shows the use of a timer function (int 15h / 86h) 2 | ; this code prints some chars with 1 second delay. 3 | 4 | ; note: Windows XP does not support this interrupt (always sets CF=1), 5 | ; to test this program in real environment write it to a floppy disk using 6 | ; compiled writebin.asm. after sucessfull compilation of both files, 7 | ; type this from command prompt: writebin timer.bin 8 | 9 | ; note: floppy disk boot record will be overwritten. 10 | ; the floppy will not be useable under windows/dos until 11 | ; you reformat it, data on floppy disk may be lost. 12 | ; use empty floppy disks only. 13 | 14 | name "timer" 15 | 16 | #make_boot# 17 | org 7c00h 18 | 19 | ; set the segment registers 20 | mov ax, cs 21 | mov ds, ax 22 | mov es, ax 23 | 24 | 25 | call set_video_mode 26 | call clear_screen 27 | 28 | 29 | next_char: 30 | cmp count, 0 31 | jz stop 32 | 33 | ; print char: 34 | mov al, c1 35 | mov ah, 0eh 36 | int 10h 37 | 38 | ; next ascii char: 39 | inc c1 40 | dec count 41 | 42 | ; set 1 million microseconds interval (1 second) 43 | mov cx, 0fh 44 | mov dx, 4240h 45 | mov ah, 86h 46 | int 15h 47 | 48 | ; stop any error: 49 | jc stop 50 | 51 | jmp next_char 52 | 53 | stop: 54 | 55 | ; print message using bios int 10h/13h function 56 | mov al, 1 57 | mov bh, 0 58 | mov bl, 0010_1111b 59 | mov cx, msg_size 60 | mov dl, 4 61 | mov dh, 15 62 | mov bp, offset msg 63 | mov ah, 13h 64 | int 10h 65 | 66 | ; wait for any key... 67 | mov ah, 0 68 | int 16h 69 | 70 | 71 | int 19h ; reboot 72 | 73 | 74 | count db 10 75 | c1 db 'a' 76 | 77 | 78 | msg db "remove floppy disk and press any key to reboot..." 79 | msg_size = $ - msg 80 | 81 | 82 | 83 | ; set video mode and disable blinking (for compatibility). 84 | set_video_mode proc 85 | mov ah, 0 86 | mov al, 3 ; text mode 80x25, 16 colors, 8 pages 87 | int 10h 88 | ; blinking disabled for compatibility with dos, 89 | ; emulator and windows prompt do not blink anyway. 90 | mov ax, 1003h 91 | mov bx, 0 ; disable blinking. 92 | int 10h 93 | ret 94 | set_video_mode endp 95 | 96 | 97 | 98 | 99 | ; clear the screen by scrolling entire screen window, 100 | ; and set cursor position on top. 101 | ; default attribute is changed to black on white. 102 | clear_screen proc near 103 | push ax ; store registers... 104 | push ds ; 105 | push bx ; 106 | push cx ; 107 | push di ; 108 | 109 | mov ax, 40h 110 | mov ds, ax ; for getting screen parameters. 111 | mov ah, 06h ; scroll up function id. 112 | mov al, 0 ; scroll all lines! 113 | mov bh, 1111_0000b ; attribute for new lines. 114 | mov ch, 0 ; upper row. 115 | mov cl, 0 ; upper col. 116 | mov di, 84h ; rows on screen -1, 117 | mov dh, [di] ; lower row (byte). 118 | mov di, 4ah ; columns on screen, 119 | mov dl, [di] 120 | dec dl ; lower col. 121 | int 10h 122 | 123 | ; set cursor position to top 124 | ; of the screen: 125 | mov bh, 0 ; current page. 126 | mov dl, 0 ; col. 127 | mov dh, 0 ; row. 128 | mov ah, 02 129 | int 10h 130 | 131 | pop di ; re-store registers... 132 | pop cx ; 133 | pop bx ; 134 | pop ds ; 135 | pop ax ; 136 | 137 | ret 138 | clear_screen endp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 8086 Programs for Evaluation 2 | 3 | ## [Arithmetic](https://github.com/jacobjohn2016/8086-Programs/tree/master/Arithmetic) 4 | 1. Addition of 2 8b numbers [add_8b_2.asm](Arithmetic/add_8b_2.asm) 5 | 2. Subtraction [sub_8b.asm](Arithmetic/sub_8b.asm) 6 | 3. Multiplication [multiply_8b.asm](/Arithmetic/multiply_8b.asm) 7 | 4. Division of 16b with 8b number [divide_16b_by_8b.asm](/Arithmetic/divide_16b_by_8b.asm) 8 | 5. Addition of 2 16b [add_8b_16b.asm](Arithmetic/add_8b_16b.asm) 9 | 6. Multiplication of 2 32b [multiply_2_32b.asm](/Arithmetic/multiply_2_32b.asm) 10 | 7. Sum of n 8b [Sum_of_n_8b.asm](/Arithmetic/Sum_of_n_8b.asm) 11 | 8. Print array [print_array.asm](Arithmetic/print_array.asm) 12 | 9. Add two 8bit numbers [add_8b_2.asm](/Arithmetic/add_8b_2.asm) 13 | 10. Load Effective Address [lea.asm](/Arithmetic/lea.asm) 14 | 11. Offset [offset.asm](/Arithmetic/offset.asm) 15 | 12. Count number of 1s in a binary number [count_1s.asm](/Arithmetic/count_1s.asm) 16 | 13. Find the largest number among 5 grades [find_largest.asm](/Arithmetic/find_largest.asm) 17 | 14. Divide 16b by 8b [divide_16b_by_8b.asm](/Arithmetic/divide_16b_by_8b.asm) 18 | 15. Add 16b with carry [add_16b_carry.asm](/Arithmetic/add_16b_carry.asm) 19 | 16. Add 16b BCD [add_16b_bcd.asm](/Arithmetic/add_16b_bcd.asm) 20 | 17. Decimal Adjust after addition [daa.asm](/Arithmetic/daa.asm) 21 | 22 | ## [Expression](https://github.com/jacobjohn2016/8086-Programs/tree/master/Expression) 23 | 18. ALP to find the Greatest Common Divisor of two unsigned integer.[gcd_two.asm](/Expression/gcd_two.asm) 24 | 19. ALP to find the Sum and average of unsigned integer. [sum_average_unsigned.asm](/Expression/sum_average_unsigned.asm) 25 | 20. Develop and execute an ALP to compute factorial of a positive integer number using recursive procedure. [fact.asm](/Expression/fact.asm) 26 | 21. Transfer string from one memory location to another. [copy_string_memory_location.asm](/Expression/copy_string_memory_location.asm) 27 | 22. Count vowels in a word [count_vowels.asm](/Expression/count_vowels.asm) 28 | 23. Calculate power(a,b) i.e a^b [power.asm](/Expression/power.asm) 29 | 24. Average of values stored in an array. [average_sum_array.asm](/Expression/average_sum_array.asm) 30 | 25. Find Reverse of an array. [reverse_array.asm](/Expression/reverse_array.asm) 31 | 26. Prompts the user to enter an array and displays it [prompt_user_array+display.asm](/Expression/prompt_user_array%2Bdisplay.asm) 32 | 27. Find largest number in memory location [finding_largest_number_memory.asm](/Expression/finding_largest_number_memory.asm) 33 | 28. Check if number is even or odd [check_number_even_odd.asm](/Expression/check_number_even_odd.asm) 34 | 29. Check if given number is prime [given_number_prime.asm](/Expression/given_number_prime.asm) 35 | 30. Fibonacci Sequence [fibonacci.asm](/Expression/fibonacci.asm) || [fibonacci2.asm](/Expression/fibonacci2.asm) || [fibonacci3.asm](/Expression/fibonacci3.asm) 36 | 31. Concatenation of strings [concatenation_string.asm](/Expression/concatenation_string.asm) 37 | 32. Check if string contains substring [substring_in_string.asm](/Expression/substring_in_string.asm) 38 | 33. Count number of words [count_words.asm](/Expression/count_words.asm) 39 | 40 | ## [Conversion](https://github.com/jacobjohn2016/8086-Programs/tree/master/Conversion) 41 | 34. ALP for conversion of 16-bit HEX number into its equivalent BCD number.[hex_bcd.asm](/Conversion/hex_bcd.asm) 42 | 35. ALP for conversion of 16-bit BCD number into its equivalent HEX number. [bcd_hex.asm](/Conversion/bcd_hex.asm) 43 | 36. ALP for conversion BCD number 7-Segment String. [seven_segment.asm](/Conversion/seven_segment.asm) 44 | 37. ALP to copy the string of successive memory locations from one memory to other. 45 | 46 | a. Using string instructions [copy_string_instruction.asm](/Conversion/copy_string_instruction.asm) 47 | 48 | b. Without using string instruction [copy_without_string_instruction.asm](/Conversion/copy_without_string_instruction.asm) 49 | 38. Compare two strings [compare_strings.asm](/Conversion/compare_strings.asm) 50 | 39. Reverse a number [reverse_number.asm](/Conversion/reverse_number.asm) 51 | 40. Decimal to binary [decimal_to_binary.asm](/Conversion/decimal_to_binary.asm) 52 | 41. Decimal to octal [decimal_to_octal.asm](/Conversion/decimal_to_octal.asm) 53 | 42. Hexadecimal to decimal [hex_to_decimal.asm](/Conversion/hex_to_decimal.asm) 54 | 55 | ## [Sorting](https://github.com/jacobjohn2016/8086-Programs/tree/master/Sorting) 56 | 43. ALP to Sort a set of unsigned integer numbers in ascending/ descending order using Bubble sort algorithm. [bubble_sort.asm](/Sorting/bubble_sort.asm) 57 | 44. Array Ascending [array_ascending.asm](/Sorting/array_ascending.asm) 58 | 45. Array Descending [array_descending.asm](/Sorting/array_descending.asm) 59 | 60 | ## [Searching](https://github.com/jacobjohn2016/8086-Programs/tree/master/Searching) 61 | 46. Develop and execute ALP that implements Binary search algorithm. The data consists of sorted 16 bit unsigned integers. The search key is also a 16 bit unsigned integer. [binary_search.asm](/Searching/binary_search.asm) 62 | 47. Search Element in an array [search_element_array.asm](/Searching/search_element_array.asm) 63 | 48. Linear Search [linear_search.asm](/Searching/linear_search.asm) 64 | 49. Occurences of character [occurences_character_count.asm](/Searching/occurences_character_count.asm) 65 | 66 | # Assessment Programs 67 | 68 | 1. Emulate a counter on emu8086, to count the no. of 1’s (binary) in the given input value. [count_1s.asm](/Arithmetic/count_1s.asm) 69 | 2. Emulate water level controller on emu8086 for the following Specifications: [water_level_controller.asm](/Simulation/water_level_controller.asm) 70 | * No. of water levels in the overhead tank is 8 71 | * Display the current level of water with a buzzer 72 | * Switch on the motor if the water level is 1 73 | * Switch off the motor if the water level is 8 74 | * Switch on the buzzer on water overflow 75 | 3. Emulate a fire monitoring system on emu8086 for the following specifications: [fire_monitoring_system.asm](/Simulation/fire_monitoring_system.asm) 76 | * Define the threshold for the temperature of two rooms 77 | * Generate the temperature value in 8b resolution 78 | * Switch on the alarm and display an alarm message when the threshold of either of the room is reached 79 | * Remove the alarm and bring the temperature below the threshold 80 | 4. Design and Emulate a smart automation system for a garment manufacturing unit with the following requirements:- [garment_defect.asm](/Simulation/garment_defect.asm) 81 | * To detect all possible defects. 82 | * To remove the defective pieces. 83 | * To provide comprehensive inventory report. 84 | 5. Find the largest number from an unordered array of sixteen 8-bit numbers stored sequentially in memory location 2000:5000H. Store the largest number in memory location 2000:5000H.[finding_largest_number_memory.asm](/Expression/finding_largest_number_memory.asm) 85 | 86 | # [External add-on devices](https://github.com/jacobjohn2016/8086-Programs/tree/master/External%20Devices) 87 | 88 | *__Note:__ All the given programs must be emulated using emu8086 only.* 89 | [Download link](http://www.emu8086.com) 90 | 91 | 1. Traffic Lights 92 | * [traffic_lights](/External%20Devices/traffic_lights.asm) 93 | * [traffic_lights2](/External%20Devices/traffic_lights2.asm) 94 | 2. LED display [LED_display_test.asm](/External%20Devices/LED_display_test.asm) 95 | 3. Stepper Motor [stepper_motor.asm](/External%20Devices/stepper_motor.asm) 96 | 4. Thermometer [thermometer.asm](/External%20Devices/thermometer.asm) 97 | 5. Robot [robot.asm](/External%20Devices/robot.asm) 98 | 6. Timer [timer.asm](/External%20Devices/timer.asm) 99 | 7. Keyboard [keybrd.asm](/External%20Devices/keybrd.asm) 100 | 8. Mouse [mouse.asm](/External%20Devices/mouse.asm) 101 | -------------------------------------------------------------------------------- /Expression/prompt_user_array+display.asm: -------------------------------------------------------------------------------- 1 | ;Code for Program that prompts the user to enter an array of size 10 and display it in Assembly Language 2 | .MODEL SMALL 3 | .STACK 100H 4 | 5 | .DATA 6 | PROMPT_1 DB 'Enter the Array elements :',0DH,0AH,'$' 7 | PROMPT_2 DB 'The Array elements are : $' 8 | 9 | ARRAY DW 10 DUP(0) 10 | 11 | .CODE 12 | MAIN PROC 13 | MOV AX, @DATA ; initialize DS 14 | MOV DS, AX 15 | 16 | MOV BX, 10 ; set BX=10 17 | 18 | LEA DX, PROMPT_1 ; load and display the string PROMPT_1 19 | MOV AH, 9 20 | INT 21H 21 | 22 | LEA SI, ARRAY ; set SI=offset address of ARRAY 23 | 24 | CALL READ_ARRAY ; call the procedure READ_ARRAY 25 | 26 | LEA DX, PROMPT_2 ; load and display the string PROMPT_2 27 | MOV AH, 9 28 | INT 21H 29 | 30 | LEA SI, ARRAY ; set SI=offset address of ARRAY 31 | 32 | CALL PRINT_ARRAY ; call the procedure PRINT_ARRAY 33 | 34 | MOV AH, 4CH ; return control to DOS 35 | INT 21H 36 | MAIN ENDP 37 | 38 | ;**************************************************************************; 39 | ;**************************************************************************; 40 | ;------------------------- Procedure Definitions ------------------------; 41 | ;**************************************************************************; 42 | ;**************************************************************************; 43 | 44 | ;**************************************************************************; 45 | ;----------------------------- READ_ARRAY -------------------------------; 46 | ;**************************************************************************; 47 | 48 | READ_ARRAY PROC 49 | ; this procedure will read the elements for an array 50 | ; input : SI=offset address of the array 51 | ; : BX=size of the array 52 | ; output : none 53 | 54 | PUSH AX ; push AX onto the STACK 55 | PUSH CX ; push CX onto the STACK 56 | PUSH DX ; push DX onto the STACK 57 | 58 | MOV CX, BX ; set CX=BX 59 | 60 | @READ_ARRAY: ; loop label 61 | CALL INDEC ; call the procedure INDEC 62 | 63 | MOV [SI], AX ; set [SI]=AX 64 | ADD SI, 2 ; set SI=SI+2 65 | 66 | MOV DL, 0AH ; line feed 67 | MOV AH, 2 ; set output function 68 | INT 21H ; print a character 69 | LOOP @READ_ARRAY ; jump to label @READ_ARRAY while CX!=0 70 | 71 | POP DX ; pop a value from STACK into DX 72 | POP CX ; pop a value from STACK into CX 73 | POP AX ; pop a value from STACK into AX 74 | 75 | RET ; return control to the calling procedure 76 | READ_ARRAY ENDP 77 | 78 | ;**************************************************************************; 79 | ;----------------------------- PRINT_ARRAY ------------------------------; 80 | ;**************************************************************************; 81 | 82 | PRINT_ARRAY PROC 83 | ; this procedure will print the elements of a given array 84 | ; input : SI=offset address of the array 85 | ; : BX=size of the array 86 | ; output : none 87 | 88 | PUSH AX ; push AX onto the STACK 89 | PUSH CX ; push CX onto the STACK 90 | PUSH DX ; push DX onto the STACK 91 | 92 | MOV CX, BX ; set CX=BX 93 | 94 | @PRINT_ARRAY: ; loop label 95 | MOV AX, [SI] ; set AX=AX+[SI] 96 | 97 | CALL OUTDEC ; call the procedure OUTDEC 98 | 99 | MOV AH, 2 ; set output function 100 | MOV DL, 20H ; set DL=20H 101 | INT 21H ; print a character 102 | 103 | ADD SI, 2 ; set SI=SI+2 104 | LOOP @PRINT_ARRAY ; jump to label @PRINT_ARRAY while CX!=0 105 | 106 | POP DX ; pop a value from STACK into DX 107 | POP CX ; pop a value from STACK into CX 108 | POP AX ; pop a value from STACK into AX 109 | 110 | RET ; return control to the calling procedure 111 | PRINT_ARRAY ENDP 112 | 113 | ;**************************************************************************; 114 | ;------------------------------- INDEC ----------------------------------; 115 | ;**************************************************************************; 116 | 117 | INDEC PROC 118 | ; this procedure will read a number indecimal form 119 | ; input : none 120 | ; output : store binary number in AX 121 | 122 | PUSH BX ; push BX onto the STACK 123 | PUSH CX ; push CX onto the STACK 124 | PUSH DX ; push DX onto the STACK 125 | 126 | JMP @READ ; jump to label @READ 127 | 128 | @SKIP_BACKSPACE: ; jump label 129 | MOV AH, 2 ; set output function 130 | MOV DL, 20H ; set DL=' ' 131 | INT 21H ; print a character 132 | 133 | @READ: ; jump label 134 | XOR BX, BX ; clear BX 135 | XOR CX, CX ; clear CX 136 | XOR DX, DX ; clear DX 137 | 138 | MOV AH, 1 ; set input function 139 | INT 21H ; read a character 140 | 141 | CMP AL, "-" ; compare AL with "-" 142 | JE @MINUS ; jump to label @MINUS if AL="-" 143 | 144 | CMP AL, "+" ; compare AL with "+" 145 | JE @PLUS ; jump to label @PLUS if AL="+" 146 | 147 | JMP @SKIP_INPUT ; jump to label @SKIP_INPUT 148 | 149 | @MINUS: ; jump label 150 | MOV CH, 1 ; set CH=1 151 | INC CL ; set CL=CL+1 152 | JMP @INPUT ; jump to label @INPUT 153 | 154 | @PLUS: ; jump label 155 | MOV CH, 2 ; set CH=2 156 | INC CL ; set CL=CL+1 157 | 158 | @INPUT: ; jump label 159 | MOV AH, 1 ; set input function 160 | INT 21H ; read a character 161 | 162 | @SKIP_INPUT: ; jump label 163 | 164 | CMP AL, 0DH ; compare AL with CR 165 | JE @END_INPUT ; jump to label @END_INPUT 166 | 167 | CMP AL, 8H ; compare AL with 8H 168 | JNE @NOT_BACKSPACE ; jump to label @NOT_BACKSPACE if AL!=8 169 | 170 | CMP CH, 0 ; compare CH with 0 171 | JNE @CHECK_REMOVE_MINUS ; jump to label @CHECK_REMOVE_MINUS if CH!=0 172 | 173 | CMP CL, 0 ; compare CL with 0 174 | JE @SKIP_BACKSPACE ; jump to label @SKIP_BACKSPACE if CL=0 175 | JMP @MOVE_BACK ; jump to label @MOVE_BACK 176 | 177 | @CHECK_REMOVE_MINUS: ; jump label 178 | 179 | CMP CH, 1 ; compare CH with 1 180 | JNE @CHECK_REMOVE_PLUS ; jump to label @CHECK_REMOVE_PLUS if CH!=1 181 | 182 | CMP CL, 1 ; compare CL with 1 183 | JE @REMOVE_PLUS_MINUS ; jump to label @REMOVE_PLUS_MINUS if CL=1 184 | 185 | @CHECK_REMOVE_PLUS: ; jump label 186 | 187 | CMP CL, 1 ; compare CL with 1 188 | JE @REMOVE_PLUS_MINUS ; jump to label @REMOVE_PLUS_MINUS if CL=1 189 | JMP @MOVE_BACK ; jump to label @MOVE_BACK 190 | 191 | @REMOVE_PLUS_MINUS: ; jump label 192 | MOV AH, 2 ; set output function 193 | MOV DL, 20H ; set DL=' ' 194 | INT 21H ; print a character 195 | 196 | MOV DL, 8H ; set DL=8H 197 | INT 21H ; print a character 198 | 199 | JMP @READ ; jump to label @READ 200 | 201 | @MOVE_BACK: ; jump label 202 | 203 | MOV AX, BX ; set AX=BX 204 | MOV BX, 10 ; set BX=10 205 | DIV BX ; set AX=AX/BX 206 | 207 | MOV BX, AX ; set BX=AX 208 | 209 | MOV AH, 2 ; set output function 210 | MOV DL, 20H ; set DL=' ' 211 | INT 21H ; print a character 212 | 213 | MOV DL, 8H ; set DL=8H 214 | INT 21H ; print a character 215 | 216 | XOR DX, DX ; clear DX 217 | DEC CL ; set CL=CL-1 218 | 219 | JMP @INPUT ; jump to label @INPUT 220 | 221 | @NOT_BACKSPACE: ; jump label 222 | 223 | INC CL ; set CL=CL+1 224 | 225 | CMP AL, 30H ; compare AL with 0 226 | JL @ERROR ; jump to label @ERROR if AL<0 227 | 228 | CMP AL, 39H ; compare AL with 9 229 | JG @ERROR ; jump to label @ERROR if AL>9 230 | 231 | AND AX, 000FH ; convert ascii to decimal code 232 | 233 | PUSH AX ; push AX onto the STACK 234 | 235 | MOV AX, 10 ; set AX=10 236 | MUL BX ; set AX=AX*BX 237 | MOV BX, AX ; set BX=AX 238 | 239 | POP AX ; pop a value from STACK into AX 240 | 241 | ADD BX, AX ; set BX=AX+BX 242 | JS @ERROR ; jump to label @ERROR if SF=1 243 | JMP @INPUT ; jump to label @INPUT 244 | 245 | @ERROR: ; jump label 246 | 247 | MOV AH, 2 ; set output function 248 | MOV DL, 7H ; set DL=7H 249 | INT 21H ; print a character 250 | 251 | XOR CH, CH ; clear CH 252 | 253 | @CLEAR: ; jump label 254 | MOV DL, 8H ; set DL=8H 255 | INT 21H ; print a character 256 | 257 | MOV DL, 20H ; set DL=' ' 258 | INT 21H ; print a character 259 | 260 | MOV DL, 8H ; set DL=8H 261 | INT 21H ; print a character 262 | LOOP @CLEAR ; jump to label @CLEAR if CX!=0 263 | 264 | JMP @READ ; jump to label @READ 265 | 266 | @END_INPUT: ; jump label 267 | 268 | CMP CH, 1 ; compare CH with 1 269 | JNE @EXIT ; jump to label @EXIT if CH!=1 270 | NEG BX ; negate BX 271 | 272 | @EXIT: ; jump label 273 | 274 | MOV AX, BX ; set AX=BX 275 | 276 | POP DX ; pop a value from STACK into DX 277 | POP CX ; pop a value from STACK into CX 278 | POP BX ; pop a value from STACK into BX 279 | 280 | RET ; return control to the calling procedure 281 | INDEC ENDP 282 | 283 | ;**************************************************************************; 284 | ;-------------------------------- OUTDEC --------------------------------; 285 | ;**************************************************************************; 286 | 287 | OUTDEC PROC 288 | ; this procedure will display a decimal number 289 | ; input : AX 290 | ; output : none 291 | 292 | PUSH BX ; push BX onto the STACK 293 | PUSH CX ; push CX onto the STACK 294 | PUSH DX ; push DX onto the STACK 295 | 296 | CMP AX, 0 ; compare AX with 0 297 | JGE @START ; jump to label @START if AX>=0 298 | 299 | PUSH AX ; push AX onto the STACK 300 | 301 | MOV AH, 2 ; set output function 302 | MOV DL, "-" ; set DL='-' 303 | INT 21H ; print the character 304 | 305 | POP AX ; pop a value from STACK into AX 306 | 307 | NEG AX ; take 2's complement of AX 308 | 309 | @START: ; jump label 310 | 311 | XOR CX, CX ; clear CX 312 | MOV BX, 10 ; set BX=10 313 | 314 | @OUTPUT: ; loop label 315 | XOR DX, DX ; clear DX 316 | DIV BX ; divide AX by BX 317 | PUSH DX ; push DX onto the STACK 318 | INC CX ; increment CX 319 | OR AX, AX ; take OR of Ax with AX 320 | JNE @OUTPUT ; jump to label @OUTPUT if ZF=0 321 | 322 | MOV AH, 2 ; set output function 323 | 324 | @DISPLAY: ; loop label 325 | POP DX ; pop a value from STACK to DX 326 | OR DL, 30H ; convert decimal to ascii code 327 | INT 21H ; print a character 328 | LOOP @DISPLAY ; jump to label @DISPLAY if CX!=0 329 | 330 | POP DX ; pop a value from STACK into DX 331 | POP CX ; pop a value from STACK into CX 332 | POP BX ; pop a value from STACK into BX 333 | 334 | RET ; return control to the calling procedure 335 | OUTDEC ENDP 336 | 337 | ;**************************************************************************; 338 | ;--------------------------------------------------------------------------; 339 | ;**************************************************************************; 340 | 341 | END MAIN --------------------------------------------------------------------------------