Docsity

8086 Assembly Programming: Basic Arithmetic and String Operations, Cheat Sheet of Computer Architecture and Organization

Toggle sidebar
bg1
8086 Assembly Programs-1
Program to add two 8 bit numbers:
CODE:
code segment
assume cs:code
start:
mov ah, 0000h
mov si,3000h
mov al,[si]
inc si
mov bl,[si]
add al,bl
inc si
mov [si],al
jc l1
inc si
mov [si],ah
int 3 ; used for singnaling that a breakpoint has been hit in debug
l1:
inc si
mov [si],ah
int 3
code ends
end start
Output:
Subtraction of two numbers:
CODE:
code segment
assume cs:code
start:
mov ah, 0000h
mov si,3000h
mov al,[si]
inc si
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download 8086 Assembly Programming: Basic Arithmetic and String Operations and more Cheat Sheet Computer Architecture and Organization in PDF only on Docsity!

8086 Assembly Programs-

Program to add two 8 bit numbers: CODE: code segment assume cs:code start: mov ah, 0000h mov si,3000h mov al,[si] inc si mov bl,[si] add al,bl inc si mov [si],al jc l inc si mov [si],ah int 3 ; used for singnaling that a breakpoint has been hit in debug l1: inc si mov [si],ah int 3 code ends end start Output: Subtraction of two numbers: CODE: code segment assume cs:code start: mov ah, 0000h mov si,3000h mov al,[si] inc si

mov bl,[si] sub al,bl inc si mov [si],al jc l inc si mov [si],ah int 3 l1: inc si mov [si],ah int 3 code ends end start Output: Multiplication of two numbers: CODE: code segment assume cs:code start: mov ax, 0000h mov si, 3000h mov al, byte ptr [si] inc si mov bl, byte ptr [si] mul bl inc si mov byte ptr [si], al inc si mov byte ptr [si], ah int 3 code ends end start Output:

mov al, byte ptr [si] inc si mov bl, byte ptr [si] and al, bl inc si mov byte ptr [si], al inc si mov byte ptr [si], 00h int 3 code ends end start Output: Program to perform logical OR operation CODE: code segment assume cs:code start: mov si, 3000h mov al, byte ptr [si] inc si mov bl, byte ptr [si] or al, bl mov si, 3002h mov byte ptr [si], al inc si mov byte ptr [si], 00h int 3 code ends end start Output:

Taking input of string and outputting a string CODE: .model small .stack 100h .data prompt db 'Enter your name: $' buffer db 100 dup('$') ; Buffer to store input string str db 'Hello, $' .code main proc mov ax, @data mov ds, ax ; Display prompt mov ah, 09h ; 09 is used to print a string stopping at $ lea dx, prompt ; loads the effective prompt address in dx int 21h ; performs the interrupt operation according to data in ah ; Read string from user mov ah, 0Ah ; for reading string 0A is used lea dx, buffer ; loads effective address of buffer inside ds int 21h ; Move to new line mov ah, 02h ; prints character present in dl mov dl, 0Dh ; moves cursor to start int 21h mov dl, 0Ah ; moves cursor to next line int 21h ; Display str mov ah, 09h lea dx, str

56H Rename file. A program that reads a file and displays the contents of that file CODE: .model small .stack 100h .DATA MESS1 DB 0AH,0DH,07H,'PLEASE ENTER FILE NAME $' MESS2 DB 0AH,0DH,07H,'SORRY FILE OPENING ERROR $' MESS3 DB 0AH,0DH,07H,'S0RRY FILE READING ERROR$' MESS4 DB 0AH,0DH,07H,'SORRY FILE CLOSING ERROR$' STRING DB 30 DUP(?) HANDLE DW? CHAR DB? DISPLAY MACRO DISP LEA DX,DISP MOV AH,09H INT 21H ENDM .CODE ;----------------- ;READ FILE NAME ;----------------- MOV AX,@data MOV DS,AX DISPLAY MESS MOV AH,0AH ;PLACE KEYBOARD CHARACTER INTO INPUT BUFFER MOV DX,OFFSET STRING ;START OF THE INPUT BUFFER MOV STRING,27 ;MAXIMUM LENGTH OF THE STRING INT 21H MOV BX,DX ;LENGTH OF THE STRING ACTUALLY ENTERED MOV AL,STRING+1 ; SUB AH,AH ;CLEAR AH ADD BX,AX MOV BYTE PTR [BX+2],0 ;LAST CHARACTER OF THE STRING 00H ; New Line

MOV AH,02H

MOV DL,0DH

INT 21H

MOV DL,0AH

INT 21H

;OPEN THE FILE

MOV AL,0 ;MODE OF ACCESS

MOV AH,3DH ;OPEN A FILE

MOV DX,OFFSET STRING+2 ;IDENTIFY THE FILE

INT 21H

INC

JC ERROR

MOV HANDLE,AX ;UNLESS ERROR OCCURS AX CONTAINS THE FILE HANDLE

;READ THE FILE CHAR BY CHAR

LOOP1: MOV CX,1 ;THE NUMBER OF BYTES TO READ

MOV BX,HANDLE ;FILE HANDLE RETURNED BY OPEN FILE

MOV AH,3FH ;READ FROM A FILE

MOV DX,OFFSET CHAR ;BYTE OF MEMORY TO PLACE THE INFORMATION

INT 21H

JC ERROR

CMP AX,0 ;ACTUAL NUMBER OF BYTES TO READ 00H FOR E.O.F.

JE CLOSE

; DISPLAY THE CHAR ON SCREEN

MOV DL,CHAR ;ASCII CODE FOR THE CHARACTER TO BE DISPLAYED

MOV AH,02H ;DISPLAY THE CHARACTER AT THE O/P DEVICE

INT 21H

JMP LOOP

; CLOSING THE FILE

CLOSE: MOV BX,HANDLE

MOV AH,3EH ;CLOSE A FILE HANDLE(UPDATE FILE DIRECTORY, FLUSHES

INT 21H ;ALL INTERNAL BUFFERS ASSOCIATED WITH THE FILE)

JC ERROR

JMP QUIT

;ERROR MESSAGE