Professional Documents
Culture Documents
1. Write a program to add two numbers present in two consecutive memory locations and
store the result in next memory location.
Code:
; multi-segment executable file template.
data segment
; add your data here!
Number1 dw 1
Number2 dw 2
Number3 dw ?
ends
stack segment
dw 128 dup(0)
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
mov ax,Number1
add ax,Number2
mov Number3,ax
Output:
2. Develop program to read a character from console and echo it.
Code:
; multi-segment executable file template.
data segment
; add your data here!
ends
stack segment
dw 128 dup(0)
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
Output:
3. Develop and execute a program to read 10 chars from console
Code:
include 'emu8086.inc'
; multi-segment executable file template.
data segment
; add your data here!
var db ?
ends
stack segment
dw 128 dup(0)
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
mov cx,10
mov ah, 1
int 21h
loop back
Output:
4. Write a program to exchange two memory variables using MOV and XCHG instruction.
Can you do it with just XCHG?
Code:
; multi-segment executable file template.
data segment
; add your data here!
var1 dw 1
var2 dw 2
ends
stack segment
dw 128 dup(0)
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
mov ax,var1
mov bx,var2
xchg ax,bx
Output:
5. Write a program to find the sum of two BCD numbers stored in memory.
Code:
varh1 dw 11h
varh2 dw 10h
varh3 dw ?
ends
stack segment
dw 128 dup(0)
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
; add your code here
mov ax,varb1 ;BCD No
add ax,varb2
mov varb3,ax
mov ax,varb1 ; hexadecimal equivalent
add ax,varb2
mov varb3,ax
; wait for any key....
mov ah, 1
int 21h
data segment
; add your data here!
var db ?
ends
stack segment
dw 128 dup(0)
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
mov ah, 1 ;read first number
int 21h
mov bl,al
sub bl,48
mov ah, 1 ;read second number
int 21h
sub al,48
mul bl
mov var,al
; mov ah, 2 ; write on screen
; mov dl,al
; int 21h
; wait for any key....
mov ah, 1
int 21h
mov ax, 4c00h ; exit to operating system.
int 21h
ends
end start ; set entry point and stop the assembler.
Output:
1. Write a program to find the factorial of decimal number given by user.
Code:
name "fact"
mov bx, cx
mov ax, 1
mov bx, 1
calc_it:
mul bx
cmp dx, 0
jne overflow
inc bx
loop calc_it
mov result, ax
print_result:
overflow:
mov dx, offset msg3
mov ah, 9
int 21h
jmp n3
msg3 db 0Dh,0Ah, 'the result is too big!', 0Dh,0Ah, 'use values
from 0 to 8.$'
n3:
jmp start
exit:
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; these functions are copied from emu8086.inc ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MOV CX, 0
; reset flag:
MOV CS:make_minus, 0
next_digit:
; add AL to CX:
MOV AH, 0
MOV DX, CX ; backup, in case the result will be
too big.
ADD CX, AX
JC too_big2 ; jump if the number is too big.
JMP next_digit
set_minus:
MOV CS:make_minus, 1
JMP next_digit
too_big2:
MOV CX, DX ; restore the backuped value before
add.
MOV DX, 0 ; DX was zero before backup!
too_big:
MOV AX, CX
DIV CS:ten ; reverse last DX:AX = AX*10, make AX =
DX:AX / 10
MOV CX, AX
PUTC 8 ; backspace.
PUTC ' ' ; clear last entered digit.
PUTC 8 ; backspace again.
JMP next_digit ; wait for Enter/Backspace.
stop_input:
; check flag:
CMP CS:make_minus, 0
JE not_minus
NEG CX
not_minus:
POP SI
POP AX
POP DX
RET
make_minus DB ? ; used as a flag.
SCAN_NUM ENDP
CMP AX, 0
JNZ not_zero
PUTC '0'
JMP printed
not_zero:
; the check SIGN of AX,
; make absolute if it's negative:
CMP AX, 0
JNS positive
NEG AX
PUTC '-'
positive:
CALL PRINT_NUM_UNS
printed:
POP AX
POP DX
RET
PRINT_NUM ENDP
; AX is zero?
CMP AX, 0
JZ print_zero
begin_print:
MOV DX, 0
DIV BX ; AX = DX:AX / BX (DX=remainder).
skip:
; calculate BX=BX/10
PUSH AX
MOV DX, 0
MOV AX, BX
DIV CS:ten ; AX = DX:AX / 10 (DX=remainder).
MOV BX, AX
POP AX
JMP begin_print
print_zero:
PUTC '0'
end_print:
POP DX
POP CX
POP BX
POP AX
RET
PRINT_NUM_UNS ENDP
Output:
1. Write a program, which takes two inputs as strings and display the Concatenated string.
Code:
;session 8-1 concatenation of two strings
data segment
str1 db "computer$"
str2 db "science$"
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
lea si,str2
; inputig the char in al
lea si,str1
back1:
cmp [si],'$'
je last1:
inc si
jmp back1
last1:
lea di,str2
back2:
cmp [di],'$'
je last2:
mov ax,[di]
mov [si],ax
inc si
inc di
jmp back2
last2:
; output the string stored the offset in DX
lea dx, str1
mov ah,9
int 21h
mov ax, 4c00h
int 21h
ends
Output:
3. Write a program for reversing a given string.
Code:
; session 8-3 reverse string
; multi-segment executable file template.
data segment
str1 db "-1234$"
str2 db ?
i db 0
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
; lower to upper
lea di,str1
back1:
cmp [di],'$'
je last1:
inc di
jmp back1
last1:
dec di
lea si,str2
; reverse string
back2:
cmp [di],'-'
je last2:
mov ax,[di]
mov [si],ax
dec di
inc si
jmp back2
last2:
mov [si],'$'
lea dx,str2
mov ah,9
int 21h
mov ax, 4c00h
int 21h
ends
end start ; set entry point and stop the assembler.
Output:
4. Write a program, which converts string to its ASCII value and store in array.
Code:
; session 8-4 convert string to ASCII
; multi-segment executable file template.
data segment
str1 dw 'F'
arr dw 4 dup(?)
i db 0
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
; Convert to ascii
mov cx,5
lea di,str1
lea si,arr
back:
mov ax,[di]
Add ax,0
aaa
mov [si],ax
inc di
inc si
loop back
mov ax, 4c00h
int 21h
ends
end start ; set entry point and stop the assembler.
Output:
5. Write a program to find if two strings are equal length: and if the strings are found to be
of equivalent length then are they the same, if not the same then which string is
lexicographically greater.
Code:
;session 8-5 string length comparision
data segment
str1 db "cmputer$"
str2 db "computer$"
msg1 db "not equal$"
msg2 db "equal$"
i1 db 0
i2 db 0
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
lea si,str2
lea si,str1
back1:
cmp [si],'$'
je last1:
inc si
inc i1
jmp back1
last1:
lea si,str2
back2:
cmp [si],'$'
je last2:
inc si
inc i2
jmp back2
last2:
mov al,i1
cmp al,i2
je last3
last4:
mov ax, 4c00h
int 21h
ends
Output:
6. Write a program to determine a given string is a palindrome. If 'Yes' output the message
“The given string is a palindrome”. If 'No' output the message “No, it is not a
palindrome”.
Code:
; this sample checks if string is a palindrome or not.
; palindrome is a text that can be read backwards
; and give the same meaning as if it was read forward.
; for example: "abba" is polindrome.
; note: this program is case sensitive, "abba" is not "abba".
name "pali"
org 100h
jmp start
m1:
s db 'able was ere ere saw elba'
s_size = $ - m1
db 0Dh,0Ah,'$'
start:
lea di, s
mov si, di
add si, s_size
dec si ; point to last char!
next_char:
mov al, [di]
mov bl, [si]
cmp al, bl
jne not_palindrome
inc di
dec si
loop next_char
is_palindrome:
; the string is "palindrome!"
mov ah, 9
mov dx, offset msg1
int 21h
jmp stop
not_palindrome:
; the string is "not palindrome!"
mov ah, 9
mov dx, offset msg2
int 21h
stop:
ret
Output: