100% found this document useful (2 votes)
319 views

Assebly Lab Print

The document contains 6 assembly language programs: 1. Adds two numbers stored in memory locations and stores the result in a third location. 2. Reads a character from the console and echoes it back. 3. Reads 10 characters from the console. 4. Exchanges the values of two memory variables using only the XCHG instruction. 5. Finds the sum of two BCD (binary-coded decimal) numbers stored in memory. 6. Reads two decimal numbers from the user, multiplies them together, and prints the result.

Uploaded by

PRATIK_DARBHE
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
319 views

Assebly Lab Print

The document contains 6 assembly language programs: 1. Adds two numbers stored in memory locations and stores the result in a third location. 2. Reads a character from the console and echoes it back. 3. Reads 10 characters from the console. 4. Exchanges the values of two memory variables using only the XCHG instruction. 5. Finds the sum of two BCD (binary-coded decimal) numbers stored in memory. 6. Reads two decimal numbers from the user, multiplies them together, and prints the result.

Uploaded by

PRATIK_DARBHE
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Sessions 3

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

; add your code here

mov ax,Number1
add ax,Number2
mov Number3,ax

; 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:
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

; add your code here

mov ah, 1 ;read from user


int 21h

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:
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

; add your code here


back:

mov ah, 1
int 21h

loop back

; output string at ds:dx

; 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:
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

; add your code here

mov ax,var1
mov bx,var2
xchg ax,bx

; 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:
5. Write a program to find the sum of two BCD numbers stored in memory.
Code:

; multi-segment executable file template.


data segment
; add your data here!
varb1 dw 00010001b
varb2 dw 00010000b
varb3 dw ?

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

mov ax, 4c00h ; exit to operating system.


int 21h
ends
end start ; set entry point and stop the assembler.
Output:
6. Write a program, which will read two decimal numbers, then multiply them together,
and finally print out the result (in decimal).
Code:
; 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 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:

; this example gets the number from the user,


; and calculates factorial for it.
; supported input from 0 to 8 inclusive!

name "fact"

; this macro prints a char in AL and advances


; the current cursor position:
putc macro char
push ax
mov al, char
mov ah, 0eh
int 10h
pop ax
endm
org 100h
jmp start
result dw ?
start:
; get first number:
mov dx, offset msg1
mov ah, 9
int 21h
jmp n1
msg1 db 0Dh,0Ah, 'enter the number: $'
n1:
call scan_num
; factorial of 0 = 1:
mov ax, 1
cmp cx, 0
je print_result

; move the number to bx:


; cx will be a counter:

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:

; print result in ax:


mov dx, offset msg2
mov ah, 9
int 21h
jmp n2
msg2 db 0Dh,0Ah, 'factorial: $'
n2:
mov ax, result
call print_num_uns
jmp exit

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:

; wait for any key press:


mov ah, 0
int 16h

ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; these functions are copied from emu8086.inc ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; gets the multi-digit SIGNED number from the keyboard,


; and stores the result in CX register:
SCAN_NUM PROC NEAR
PUSH DX
PUSH AX
PUSH SI

MOV CX, 0

; reset flag:
MOV CS:make_minus, 0

next_digit:

; get char from keyboard


; into AL:
MOV AH, 00h
INT 16h
; and print it:
MOV AH, 0Eh
INT 10h

; check for MINUS:


CMP AL, '-'
JE set_minus
; check for ENTER key:
CMP AL, 0Dh ; carriage return?
JNE not_cr
JMP stop_input
not_cr:

CMP AL, 8 ; 'BACKSPACE' pressed?


JNE backspace_checked
MOV DX, 0 ; remove last digit by
MOV AX, CX ; division:
DIV CS:ten ; AX = DX:AX / 10 (DX-
rem).
MOV CX, AX
PUTC ' ' ; clear position.
PUTC 8 ; backspace again.
JMP next_digit
backspace_checked:
; allow only digits:
CMP AL, '0'
JAE ok_AE_0
JMP remove_not_digit
ok_AE_0:
CMP AL, '9'
JBE ok_digit
remove_not_digit:
PUTC 8 ; backspace.
PUTC ' ' ; clear last entered not digit.
PUTC 8 ; backspace again.
JMP next_digit ; wait for next input.
ok_digit:

; multiply CX by 10 (first time the result is zero)


PUSH AX
MOV AX, CX
MUL CS:ten ; DX:AX = AX*10
MOV CX, AX
POP AX

; check if the number is too big


; (result should be 16 bits)
CMP DX, 0
JNE too_big

; convert from ASCII code:


SUB AL, 30h

; 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

; this procedure prints number in AX,


; used with PRINT_NUM_UNS to print signed numbers:
PRINT_NUM PROC NEAR
PUSH DX
PUSH AX

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

; this procedure prints out an unsigned


; number in AX (not just a single digit)
; allowed values are from 0 to 65535 (FFFF)
PRINT_NUM_UNS PROC NEAR
PUSH AX
PUSH BX
PUSH CX
PUSH DX

; flag to prevent printing zeros before number:


MOV CX, 1

; (result of "/ 10000" is always less or equal to 9).


MOV BX, 10000 ; 2710h - divider.

; AX is zero?
CMP AX, 0
JZ print_zero

begin_print:

; check divider (if zero go to end_print):


CMP BX,0
JZ end_print

; avoid printing zeros before number:


CMP CX, 0
JE calc
; if AX<BX then result of DIV will be zero:
CMP AX, BX
JB skip
calc:
MOV CX, 0 ; set flag.

MOV DX, 0
DIV BX ; AX = DX:AX / BX (DX=remainder).

; print last digit


; AH is always ZERO, so it's ignored
ADD AL, 30h ; convert to ASCII code.
PUTC AL

MOV AX, DX ; get remainder from last div.

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

ten DW 10 ; used as multiplier/divider by


SCAN_NUM & PRINT_NUM_UNS.

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

end start ; set entry point and stop the assembler.

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

; inputig the char in al

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

lea dx, msg1


mov ah,9
int 21h
jmp last4
last3:
lea dx, msg2
mov ah,9
int 21h

last4:
mov ax, 4c00h
int 21h

ends

end start ; set entry point and stop the assembler.

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:

; first let's print it:


mov ah, 9
mov dx, offset s
int 21h

lea di, s
mov si, di
add si, s_size
dec si ; point to last char!

mov cx, s_size


cmp cx, 1
je is_palindrome ; single char is always palindrome!

shr cx, 1 ; divide by 2!

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:

; wait for any key press:


mov ah, 0
int 16h

ret

msg1 db " this is palindrome!$"


msg2 db " this is not a palindrome!$"

Output:

You might also like