Append in file in Assembly

Following is the program to append in a file in Assembly language.

TITLE Append in file in Assembly Language
.model small
.stack

.data
file db "myfile.txt",0
msg db "This is a program to append in file in Assembly language.",'$'

.code
start:
main proc
mov ax,@data
mov ds,ax

;load file handler
mov dx,offset file
mov al,1
mov ah,3dh
int 21h

;Appending File
mov bx,ax
mov cx,0
mov ah,42h
mov al,02h
int 21h

;Writing File
mov cx,lengthof msg; should have been 1 less than length of msg.
mov dx,offset msg
mov ah,40h
int 21h

;close file
mov ah,3eh
int 21h

;exit
mov ax,4c00h
int 21h
main endp
end start
end

Read Character from Screen in Assembly Language

Program to read character from console screen in assembly language.


Move cursor in Assembly Language

How to move the cursor in Assembly Language?

Solution:


Example Code:

TITLE READ WHOLE SCREEN AND COPY CONTENT INTO SAVEBUFF
.model small
.stack
.data
hi db "Hi",'$'


.code
print macro string
mov ax,0900h
mov dx,0
mov dx,offset string
int 21h
endm
;-------------------------------------------------------------------------------------------------
start:
main proc
mov ax,@data
mov ds,ax

;move cursor from where copy data will start from screen into saveBuff
mov ax,0200h
mov dx,0200h; move cursor to 3rd row of 0th column
mov bx,0; video page = 0
int 10h

print hi

mov ax,4c00h
int 21h
main endp
end start
end

Write to file in Assembly Lanugage

.model small
.stack
.data
buffer db "I'm writing.",'$'
file db "myfile.txt",0


.code
mov ax,@data
mov ds,ax
 
mov dx, offset file
;mov al,0            ;Open file (read-Only)
mov al,1            ; Write only
;mov al,2            ; Read & Write
mov ah,3dh           ; Load File handler and store in ax
int 21h

mov cx,10            ; no. of bytes to be written. I want only first 10 bytes to be written in my already created file
mov bx,ax            ; Move file Handler to bx
mov dx,offset buffer ; load offset of string which is to be written to file
mov ah,40h
int 21h

mov ah,4ch
int 21h
end

Read from file Assembly Language

.model small
.stack
.data
buffer db 5000 dup("$")
file db "myfile.txt",0


.code
mov ax,@data
mov ds,ax
 
mov dx, offset file
mov al,0            ;Open file (read-Only)
;mov al,1            ; Write only
;mov al,2            ; Read & Write
mov ah,3dh           ; Load File handler and store in ax
int 21h

mov bx,ax            ; Move file Handler to bx
mov dx,offset buffer
mov ah,3fh
int 21h

mov ah,9h
int 21h

mov ah,4ch
int 21h
end

3fh not working Assembly Language

Problem: 3fh not working in Assembly Language

Solution: Don't do mov cx,0 before calling interrupt for input.

Displaying 2 digit number Assembly


TITLE Displaying 2 digit number in Assembly
.model small
.stack
.data

.code
mov ax,2579; 2579 will be displayed
mov cx,0
mov bx,10

loop1:
cmp ax,0
je next1
mov dx,0
div bx
push dx
inc cx
jmp loop1

next1:
mov ah,2h
loop2:
pop dx
add dx,48
int 21h
loop loop2

;exit:
mov ax,0
mov ah,4ch
int 21h
end

NEG in Assembly Language


¢NEG X
X := –X


NEG in Assembly Language


TITLE NEG Command in Assembly Language
.MODEL small
.STACK 1000H

.DATA
V1 DW 7
V2 DW 2

.CODE
;LOAD DATA
MOV AX,@DATA
MOV DS,AX

;V1 = V1 + (-V2)
mov BX,V2
NEG BX
ADD V1,BX

;PRINT V1
MOV DX,V1
ADD DX,48
MOV AH,2H
INT 21H

;RETURN CONTROL
mov ah,4ch
int 21h
END

XCHG in Assembly Language



;Following program takes two numbers from user and stores them in v1 and v2 respectively, then exchanges the values of v1 and v2 using XCHG command in assembly Language on 8086.

TITLE XCHG in Assembly Language
.model small
.stack
.data
v1 db ?
v2 db ?

.code
;input first number
mov ah,1h
int 21h

mov v1,al; v1 = first number
int 21h
mov v2,al; v2 = second number


;print v1(first) number
mov ah,2h
mov dl,v1
int 21h
;print v2(2nd) number
mov dl,v2
int 21h



;Xchange values of v1 and v2
mov al,v1
xchg al,v2
mov v1,al

;print newline
mov dl,10
int 21h

;print v1(first) number
mov ah,2h
mov dl,v1
int 21h
;print v2(2nd) number
mov dl,v2
int 21h

mov ax,0
 mov ah,4ch
 int 21h
end

Print String in Assembly Language


Program to print a string in Assembly Language is given below:

;program to print a string in Assembly Language
.model small
.stack
.data
str db "Hello World!",'$'
.code
mov ax, seg str
mov ds, ax
mov ah,09
lea dx,str
int 21h

EXIT:
mov ah,4ch
int 21h
end

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Carriage Return, (ASCII 13D) is the control character to bring the cursor to the start of a line.



Line-feed (ASCII 10D) is the control character that brings the cursor down to the next line on the screen.

(We use the abbreviations CR and LF to refer to Return and Line-feed in comments.)


;How to come to new line while printing in assembly language.
.model small
.stack
.data
str db "First Line",0AH,"SECOND LINE",0AH,"3RD line",'$'
.code
MOV AX,@DATA
MOV DS,AX
MOV AH,09
MOV DX, OFFSET STR
INT 21H

mov ah,4ch
int 21h
end


Alternate way of printing Hello World in Assembly Language

Hello World Program in Assembly Language


Hello World Program in Assembly Language is given below. This program prints Hello World in Assembly Language.

TITLE Hello World Program in Assembly Language
.model small
.stack
.data
str db "Hello World!",'$'
.code
mov ax,@data
mov ds,ax
mov ah,09h
mov dx,offset str
int 21h

EXIT:
mov ah,4ch
int 21h
end

An alternate approach for printing Hello World in Assembly Language

if in Assembly Language

 Take input from user and implement condition:
 If((var > 5 ) && (var < 9))
 {
 Print 1 according to the value of var
 }
 Else
 0



.model small
.stack

.code
mov ah,1h
int 21h

mov bl,al
cmp bl,'5'
jbe below
cmp bl,'9'
jae below
;print 1 according to the value of bl
mov dl,'1'
SUB BL,48
mov cx,bx
mov ah,2h
loop1:
int 21h
loop loop1
JMP EXIT

below:
MOV DL,'0';PRINT 0
MOV AH,2H
INT 21H

EXIT:
mov ah,4ch
int 21h
end

Loop in Assembly Language


TITLE LOOP IN ASSEMBLY LANGUAGE
.model small
.stack
.data
.code
MOV CX,10
L1:
MOV DL,'A'
MOV AH,2H
INT 21H
LOOP L1

MOV AH,4CH
INT 21H
end

See another way:

Loop in Assembly Language

Loop in Assembly Language


TITLE LOOP IN ASSEMBLY LANGUAGE
.model small
.stack

.code
mov bl,10
L1:
MOV AH,2H;PRINT 'A'
MOV DL,'A'
INT 21H
DEC bL
JNZ L1

MOV AH,4CH
INT 21H
end

See another way for

Loop in Assembly Language

Loop in Assembly Language


Loop in Assembly Language

TITLE for (int i = 10; i!=0; i++)
; print 'A'

.model small
.stack

.code
;initializing
mov al,10;
mov bl,al;

;condition
wapis:
cmp bl,0;
;if (bl == 0)
je false
; else
; if (bl != 0)
; print 'A'
mov dl,'A'
mov ah,02h
;decrement
sub bl,1
int 21h
jmp wapis

false:
;Give control back to OS
mov ah,4ch
int 21h

end

Upper-Case to Lower-Case in Assembly Language Program


Program to convert upper-case to Lower-case in Assembly Language.


TITLE Write Assembly program to Input Upper Case letter from user and display it’s lower case. (ADD 32 from ASCII)

.model small
.stack

.code

;Input character
mov ah,07h;
int 21h;

;Capitalize
ADD al,32

;print character
mov ah,02h;
mov dl,al;
int 21h;

;Give control back to OS
mov ah,4ch
int 21h

end


See Also:

Lower-case to Upper-case in Assembly Language Program

Lower-case to Upper-case in Assembly Language Program

Program to convert lower-case to upper-case in Assembly Language.



TITLE Write Assembly program to Input Lower Case letter from user and display it’s upper case. (Subtract 32 in ASCII)

.model small
.stack

.code

;Input character
mov ah,07h;
int 21h;

;Capitalize
sub al,32

;print character
mov ah,02h;
mov dl,al;
int 21h;

;Give control back to OS
mov ah,4ch
int 21h

end


You might be interested in

Upper-Case to Lower-Case in Assembly Language Program

Print character in assembly language



Program to print a character in assembly language is given below.
Write character to standard output. DL = character to write, after
execution AL = DL.

TITLE Print character in assembly language

.model small
.stack

.code
mov dl,'A';
mov ah,02h;
int 21h;

mov ah,4ch;
int 21h;

end

Input character in Assembly Language


TITLE Program to Input character in Assembly Language
.model small
.stack

.code
mov ah,01h;
int 21h;

mov ah,4ch;
int 21h;
end