Last active
August 19, 2023 20:55
-
-
Save LitHaxor/2a9fb24834189357731b7beb8f8cb979 to your computer and use it in GitHub Desktop.
Basic Assembly Codes for EMU8086
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .MODEL STACK | |
| .STACK 100H | |
| .DATA | |
| ; BH BL ARE 8 BIT = 1 BYTE | |
| ; BX ARE 16 BIT = 1 WORD | |
| NUM1 DB 49 | |
| CHAR DB 'A' | |
| MSG DB "My name is x $" | |
| .CODE | |
| ; code segment | |
| MAIN PROC | |
| ; IMPORT ALL DATA TO Accumulator | |
| MOV AX , @DATA | |
| MOV DS, AX ; DS= data source registor | |
| ; INSTRUCTION DESTINATION, SOURCE | |
| ; PRINT NUMBER | |
| MOV AH, 2 ; initiate AH register | |
| MOV DL, NUM1 ; DL register used to print NUM1 is the vairable to print | |
| INT 21H | |
| ; NEW LINE | |
| MOV AH, 2 | |
| MOV DL , 0AH | |
| INT 21H | |
| MOV DL, 0DH | |
| INT 21H | |
| ; PRINT CHAR | |
| MOV AH, 2 | |
| MOV DL, CHAR | |
| INT 21H | |
| ; NEW LINE | |
| MOV AH, 2 | |
| MOV DL , 0AH | |
| INT 21H | |
| MOV DL, 0DH | |
| INT 21H | |
| ; PRINT STRING | |
| MOV AH, 9 | |
| LEA DX, MSG | |
| INT 21H | |
| ; Perform ADDDITION | |
| MOV BH, 4 | |
| MOV BL, 5 | |
| ADD BH, BL | |
| ; PRINT ADDTION | |
| MOV DL, BH | |
| INT 21H | |
| ; EXIT CODE | |
| MOV AH, 4CH | |
| INT 21H | |
| MAIN ENDP | |
| END MAIN ; RETURN 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .MODEL SMALL | |
| .STACK 100H | |
| .DATA | |
| .CODE | |
| MAIN PROC ; INT MAIN | |
| ; INPUT | |
| MOV AH, 1 | |
| INT 21H ; TAKING INPUT FROM BIOS | |
| ; THE INPUT IS STORED IN AL register | |
| MOV AH, 2 | |
| MOV DL, AL ; DL to print the value that stored in AL | |
| INT 21H | |
| ; EXIT CODE | |
| MOV AH, 4CH | |
| INT 21H | |
| MAIN ENDP | |
| END MAIN ; RETURN 0 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .MODEL SMALL | |
| .STACK 100H | |
| .DATA | |
| input_message DB "Please enter number 1= $" | |
| input_message2 DB "Please enter number 2 = $" | |
| output DB "Output is $" | |
| NEWLINE DB 0DH, 0AH, '$' | |
| .CODE | |
| MAIN PROC | |
| ; IMPORT | |
| MOV AX, @DATA | |
| MOV DS, AX | |
| ; SHOW message | |
| MOV AH, 9 | |
| LEA DX, input_message | |
| INT 21H | |
| ; INPUT A | |
| MOV AH, 1 | |
| INT 21H | |
| SUB AL, 48 ; AL = AL -48 | |
| MOV BL, AL | |
| ; PRINT NEW LINE | |
| MOV AH, 9 | |
| MOV DX, OFFSET NEWLINE | |
| INT 21H | |
| MOV AH,9 | |
| LEA DX, input_message2 | |
| INT 21H | |
| ;INPUT B | |
| MOV AH, 1 | |
| INT 21H | |
| SUB AL, 48 ; AL = AL -48 | |
| ; ADDITION | |
| ADD BL, AL ; BL = BL + AL | |
| ADD BL, 48 ; BL = BL + 48 ASCII -> Number | |
| ; NEWLINE | |
| MOV AH, 9 | |
| MOV DX, OFFSET NEWLINE | |
| INT 21H | |
| ; OUTPUT MESSAGE | |
| MOV AH, 9 | |
| LEA DX, output | |
| INT 21H | |
| ; OUTPUT A+B | |
| MOV DL, BL | |
| MOV AH, 2 ; set the ah register to match with the 02 function code (write to STDOUT) | |
| INT 21H | |
| ; EXIT | |
| MOV AH, 4CH | |
| INT 21H | |
| MAIN ENDP | |
| END MAIN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .MODEL SMALL | |
| .STACK 100H | |
| .DATA | |
| INPUT_MESSAGE DB "Input: $" | |
| INVALID_MESSAGE DB 0DH, 0AH, "WRONG INPUT! ALPHABETS ONLY..", 0DH, 0AH, "$" | |
| VALID_MESSAGE DB 0DH, 0AH, "CORRECT ORDER IS: $" | |
| .CODE | |
| MAIN PROC | |
| MOV AX, @DATA | |
| MOV DS, AX | |
| ; PRINT INPUT | |
| MOV AH, 9 | |
| LEA DX, INPUT_MESSAGE | |
| INT 21H | |
| TAKE_INPUT: | |
| ; TAKE INPUT | |
| MOV AH, 1 | |
| INT 21H | |
| MOV BH, AL | |
| CMP BH, 0DH | |
| JE EXIT | |
| CMP BH, 'A' | |
| JB INVALID_INPUT | |
| CMP BH, 'Z' | |
| JA INVALID_INPUT | |
| MOV AH, 1 | |
| INT 21H | |
| MOV CH, AL | |
| CMP CH, 0DH | |
| JE EXIT | |
| CMP CH, 'A' | |
| JB INVALID_INPUT | |
| CMP CH, 'Z' | |
| JA INVALID_INPUT | |
| MOV AH, 9 | |
| LEA DX, VALID_MESSAGE | |
| INT 21H | |
| ; Print two input in accending | |
| CMP BH, CH | |
| JG BA | |
| JL AB | |
| JMP TAKE_INPUT | |
| AB: | |
| MOV AH, 2 | |
| MOV DL, BH | |
| INT 21H | |
| MOV AH, 2 | |
| MOV DL, CH | |
| INT 21H | |
| JMP EXIT | |
| BA: | |
| MOV AH,2 | |
| MOV DL, CH | |
| INT 21H | |
| MOV AH, 2 | |
| MOV DL, BH | |
| INT 21H | |
| JMP EXIT | |
| INVALID_INPUT: | |
| MOV AH, 9 | |
| LEA DX, INVALID_MESSAGE | |
| INT 21H | |
| MOV AH, 9 | |
| LEA DX, INPUT_MESSAGE | |
| INT 21H | |
| JMP TAKE_INPUT | |
| EXIT: | |
| MOV AH, 4CH | |
| INT 21H | |
| MAIN ENDP | |
| END MAIN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .stack 100h | |
| .model small | |
| .data | |
| m1 DB "Enter char: $" | |
| m2 DB 0AH, 0DH, "ASCII: $" | |
| m4 DB 0AH, 0DH, "Count of 0 Bits: $" | |
| m5 DB 0AH, 0DH, "Count of 1 Bits: $" | |
| c1 DB ? | |
| c2 DB ? | |
| .code | |
| main proc | |
| mov ax, @data | |
| mov ds, ax | |
| mov ah, 9 | |
| lea dx, m1 | |
| int 21h | |
| mov ah, 1 | |
| int 21h | |
| mov c1, al | |
| mov c2, al | |
| mov ah, 9 | |
| lea dx, m2 | |
| int 21H | |
| mov ah, 2 | |
| mov cx, 8 | |
| mov bl, 0 ; Initialize counter for 0 bits | |
| mov bh, 0 ; Initialize counter for 1 bits | |
| TOP: | |
| SHL c1, 1 | |
| JC set | |
| MOV DL, '0' | |
| INT 21H | |
| INC bl ; Increment 0 bits counter | |
| JMP LABEL | |
| set: | |
| MOV AH, 2 | |
| MOV DL, '1' | |
| INT 21H | |
| INC bh ; Increment 1 bits counter | |
| JMP TOP | |
| LABEL: | |
| LOOP TOP | |
| mov ah, 9 | |
| lea dx, m4 | |
| int 21H | |
| mov ah, 2 | |
| mov dl, bl | |
| add dl, 30H | |
| int 21H | |
| mov ah, 9 | |
| lea dx, m5 | |
| int 21H | |
| mov ah, 2 | |
| mov dl, bh | |
| add dl, 30H | |
| int 21H | |
| mov ah, 4ch | |
| int 21h | |
| main endp | |
| end main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .model small | |
| .stack 100h | |
| .data | |
| msg_1 db "Enter input: $" | |
| msg_2 db 0AH, 0DH, "Even$", 0AH, 0DH | |
| msg_3 db 0AH, 0DH, "Odd$", 0AH, 0DH | |
| v_1 db ? | |
| .code | |
| main proc | |
| mov ax, @data | |
| mov ds, ax | |
| input: | |
| mov ah, 9 | |
| lea dx, msg_1 | |
| int 21h | |
| mov ah, 1 | |
| int 21h | |
| mov v_1, al | |
| ; Check if the input is even or odd | |
| TEST v_1, 01H | |
| JZ EVEN | |
| JMP ODD | |
| EVEN: | |
| mov ah, 9 | |
| lea dx, msg_2 | |
| int 21h | |
| JMP DONE | |
| ODD: | |
| mov ah, 9 | |
| lea dx, msg_3 | |
| int 21h | |
| DONE: | |
| mov ah, 4ch | |
| int 21h | |
| main endp | |
| end main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .model small | |
| .stack 100h | |
| .data | |
| prmt_1 DB "Enter input HEX: $" | |
| msg_1 DB 0AH, 0DH,"Binary output: $" | |
| msg_2 DB 0AH, 0DH, "1's Count: $", 0AH, 0DH | |
| msg_3 DB 0AH, 0DH, "0's Count: $", 0AH, 0DH | |
| err_1 DB 0AH, 0DH, "Error input $" , 0AH, 0DH | |
| c1 DB ? | |
| c2 DB ? | |
| .code | |
| main proc | |
| mov ax, @data | |
| mov ds, ax | |
| mov ah, 9 | |
| lea dx, prmt_1 | |
| int 21H | |
| INPUT: | |
| mov bx, 0 ; storing to bx | |
| MOV CL, 4 | |
| mov ah, 1 | |
| int 21h | |
| WHILE_: | |
| CMP AL, 0DH ; 13 or ret | |
| JE OUTPUT | |
| CMP AL, '0' | |
| JL ERROR | |
| CMP AL, '9' | |
| JG LETTER | |
| AND AL, 0FH | |
| JMP SHIFT | |
| LETTER: | |
| CMP AL, 'F' | |
| JG ERROR | |
| CMP AL, 'A' | |
| JL ERROR | |
| SUB AL, 37H | |
| SHIFT: | |
| SHL BX, 4 | |
| OR BL, AL | |
| INT 21H | |
| JMP WHILE_ | |
| OUTPUT: | |
| MOV AH, 9 | |
| LEA DX, msg_1 | |
| INT 21H | |
| MOV c1, 0 | |
| MOV c2, 0 | |
| MOV AH, 2 | |
| MOV CX, 16 | |
| TOP: | |
| SHL BX, 1 | |
| JC SET | |
| MOV DL, '0' | |
| INC c1 | |
| INT 21H | |
| JMP LABEL | |
| SET: | |
| MOV AH,2 | |
| INC c2 | |
| MOV DL,'1' | |
| INT 21H | |
| LABEL: | |
| LOOP TOP | |
| JMP EXIT | |
| ERROR: | |
| MOV AH, 9 | |
| LEA DX, err_1 | |
| int 21h | |
| EXIT: | |
| MOV AH, 9 | |
| LEA DX, msg_2 | |
| INT 21H | |
| MOV AH, 2 | |
| MOV DL, c2 | |
| ADD DL, 30H | |
| INT 21H | |
| MOV AH, 9 | |
| LEA DX, msg_3 | |
| int 21H | |
| MOV AH, 2 | |
| MOV DL, c1 | |
| int 21h | |
| MOV AH, 4CH | |
| INT 21H | |
| main endp | |
| end main | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; Reverse three characters 123, output should look like as follows: Hints, use the concept of push and pop. | |
| ; Lithaxor | |
| ; COA-2023 | |
| .model small | |
| .stack 100h | |
| .data | |
| prmt_1 db "Enter string: $" | |
| msg_1 db 0DH, 0AH, "Reversed string: $" | |
| .code | |
| main proc | |
| mov ax, @data | |
| mov ds, ax | |
| mov ah, 9 | |
| lea dx, prmt_1 | |
| int 21h | |
| mov cx, 3 | |
| TOP: | |
| mov ah, 1 | |
| int 21h | |
| push ax ; Push the character onto the stack | |
| loop TOP | |
| ; output | |
| mov ah, 9 | |
| lea dx, msg_1 | |
| int 21h | |
| mov cx, 3 ; Reset the loop counter | |
| TOP_2: | |
| pop dx ; Pop the character from the stack | |
| mov ah, 2 ; Use function 2 to print a character | |
| int 21h | |
| loop TOP_2 | |
| mov ah, 4ch | |
| int 21h | |
| main endp | |
| end main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment