mp lab

78
What is assembly language? assembly language is a low level programming language. you need to get some knowledge about computer structure in order to understand anything. the simple computer model as i see it: the system bus (shown in yellow) connects the various components of a computer. The CPU is the heart of the computer, most of computations occur inside the CPU. RAM is a place to where the programs are loaded in order to be executed. inside the CPU Page 1 of 78

Upload: crazyramarao

Post on 29-Nov-2014

182 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: mp lab

What is assembly language?

assembly language is a low level programming language. you need to get some knowledge about computer structure in order to understand anything. the simple computer model as i see it:

the system bus (shown in yellow) connects the various components of a computer.The CPU is the heart of the computer, most of computations occur inside the CPU.RAM is a place to where the programs are loaded in order to be executed.

inside the CPU

Page 1 of 65

Page 2: mp lab

general purpose registers

8086 CPU has 8 general purpose registers, each register has its own name:

AX - the accumulator register (divided into AH / AL). BX - the base address register (divided into BH / BL). CX - the count register (divided into CH / CL). DX - the data register (divided into DH / DL). SI - source index register. DI - destination index register. BP - base pointer. SP - stack pointer.

despite the name of a register, it's the programmer who determines the usage for each general purpose register. the main purpose of a register is to keep a number (variable). the size of the above registers is 16 bit, it's something like: 0011000000111001b (in binary form), or 12345 in decimal (human) form.

4 general purpose registers (AX, BX, CX, DX) are made of two separate 8 bit registers, for example if AX= 0011000000111001b, then AH=00110000b and AL=00111001b. therefore, when you modify any of the 8 bit registers 16 bit register is also updated, and vice-versa. the same is for other 3 registers, "H" is for high and "L" is for low part.

because registers are located inside the cpu, they are much faster than memory. accessing a memory location requires the use of a system bus, so it takes much longer. accessing data in a register usually takes no time. therefore, you should try to keep variables in the registers. register sets are very small and most registers have special purposes which limit their use as variables, but they are still an excellent place to store temporary data of calculations.

segment registers

CS - points at the segment containing the current program. DS - generally points at segment where variables are defined. ES - extra segment register, it's up to a coder to define its usage. SS - points at the segment containing the stack.

although it is possible to store any data in the segment registers, this is never a good idea. the segment registers have a very special purpose - pointing at accessible blocks of memory.

Page 2 of 65

Page 3: mp lab

segment registers work together with general purpose register to access any memory value. For example if we would like to access memory at the physical address 12345h (hexadecimal), we should set the DS = 1230h and SI = 0045h. This is good, since this way we can access much more memory than with a single register that is limited to 16 bit values.CPU makes a calculation of physical address by multiplying the segment register by 10h and adding general purpose register to it (1230h * 10h + 45h = 12345h):

the address formed with 2 registers is called an effective address. by default BX, SI and DI registers work with DS segment register;BP and SP work with SS segment register.Other general purpose registers cannot form an effective address! also, although BX can form an effective address, BH and BL cannot.

special purpose registers

IP - the instruction pointer. flags register - determines the current state of the microprocessor.

IP register always works together with CS segment register and it points to currently executing instruction.flags register is modified automatically by CPU after mathematical operations, this allows to determine the type of the result, and to determine conditions to transfer control to other parts of the program.generally you cannot access these registers directly, the way you can access AX and other general registers, but it is possible to change values of system registers using some tricks that you will learn a little bit later.

Page 3 of 65

Page 4: mp lab

Addition of two 16-bit numbers with carry

data segment ; starts data segment a db 84h ; first operand a b db 84h ; second operand b sumcarry dw 1 dup(0) ; sumcarry is a operand to store resultdata ends ; ends data segmentcode segment ; starts code segment assume cs:code,ds:datastart:mov ax,data ; initialize data segment mov ds,ax xor ax,ax ; clear ax register content mov al,a ; load al register with a add al,b ; add al content wit b jnc nocarry ; jump to nocarry if no carry flag set inc ah ; increment ah by 1nocarry: mov sumcarry,ax ; load ax content into sumcarry hlt ; terminate the program code ends ; ends code segmentend start ; program ends

Output:

Page 4 of 65

Page 5: mp lab

Addition of two 16-bit numbers without carry

Page 5 of 65

Page 6: mp lab

data segment ; starts data segment a dw 1234h ; first operand a b dw 5678h ; second operand b sum dw 1 dup(0) ; sum is operand to store the result carry db 1 dup(0) ; carry is operand to store the resultdata ends ; ends data segment assume cs:code,ds:datacode segment ; starts code segmentstart:mov ax,data ; initialize data segment mov ds,ax xor bl,bl ; clear bl register content mov ax,a ; load ax with a add ax,b ; add ax content with b jnc nocarry ; jump to nocarry if no carry flag set inc bl ; increment bl by 1nocarry: mov sum,ax ; load ax content into sum mov carry,bl ; load bl content into carry hlt ; terminate programcode ends ; ends code segment end start ; program ends

Output:

Page 6 of 65

Page 7: mp lab

Subtraction of two 8-bit numbers with borrow

Page 7 of 65

Page 8: mp lab

data segment ; starts data segment a db 42h ; first operand a b db 84h ; second operand b subborrow dw 1 dup(0) ; subborrow is operand to store resultdata ends ; ends data segmentassume cs:code,ds:datacode segment ; starts code segmentstart:mov ax,data ; intitialize data segment mov ds,ax xor ah,ah ; clear ah register content mov al,a ; load al with a sub al,b ; subtract b content from al jnb noborrow ; jump to noborrow in no carry flag set dec ah ; decrement ah by 1noborrow: mov subborrow,ax ; load ax content to subborrow hlt ; terminate programcode ends ; ends code segment end start ; program ends

Output:

Page 8 of 65

Page 9: mp lab

Page 9 of 65

Page 10: mp lab

Subtraction of two 16-bit numbers without borrow

data segment ; starts data segment a dw 4234h ; first operand a b dw 3456h ; second operand b subb dw 1 dup(0) ; subb is operand to store the result borrow db 1 dup(0) ; borrow is perand to store the resultdata ends ; ends data segmentassume cs:code,ds:datacode segment ; stars code segmentstart:mov ax,data ; initialize data segment mov ds,ax xor bl,bl ; clear bl register content mov ax,a ; load ax with a sub ax,b ; subtract b from ax jnb noborrow ; jump to noborrow if no carry flag set dec bl ; decrement bl by 1noborrow: mov subb,ax ; load ax content into subb mov borrow,bl ; load bl content into borrow hlt ; terminate programcode ends ; ends code segment end start ; program ends

Output:

Page 10 of 65

Page 11: mp lab

Multiplication of two 16-bit numbers

data segment ; starts data segment

Page 11 of 65

Page 12: mp lab

a dw 0024h ; first operand a b dw 0042h ; second operand b proh dw 1 dup(0) ; proh is operand to store result prol dw 1 dup(0) ; prol is operand to store resultdata ends ; ends data segmentassume cs:code,ds:datacode segment ; starts code segmentstart:mov ax,data ; initialize data segment mov ds,ax mov ax,a ; load ax with a mov bx,b ; load bx with b mul bx ; multiply with bx content mov proh,ax ; load ax content into proh mov prol,dx ; load dx content into prol hlt ; termenate programcode ends ; ends code segment end start ; program ends

Output:

Page 12 of 65

Page 13: mp lab

Division of two 32-16 bit numbers

Page 13 of 65

Page 14: mp lab

data segment ; starts data segment divl dw 1234h ; first operand divl divu dw 0000h ; second operand divu divisor dw 1234h ; third operand c coffe dw 1 dup(0) ; coffe is operand to store the result rem dw 1 dup(0) ; rem is operand to store the resultdata ends ; ends data segmentassume cs:code,ds:datacode segment ; starts code segmentstart:mov ax,data ; initialize data segment mov ds,ax mov ax,divl ; load ax with a mov dx,divu ; load dx with b mov bx,divivsor ; load bx with c div bx ; division with bx mov coffe,ax ; load ax content into coffe mov rem,dx ; load dx content into rem hlt ; terminate programcode ends ; ends code segment end start ; program ends

Output:

Page 14 of 65

Page 15: mp lab

Factorial of a given number

data segment ; data segment starts

Page 15 of 65

Page 16: mp lab

b dw 0005h ; b is first operand fact dw 1 dup(0) ; fact is operand to store resultdata ends ; data segments endsassume cs:code,ds:datacode segment ; code segment startsstart:mov ax,data ; initialize the data segment mov ds,ax mov cx,b ; load b into cx register mov ax,0001h ; load ax with 0001hl1: mov bx,cx ; load cx content into bx mul bx ; multiply with bx content mov bx,ax ; load bx with ax content dec cx ; decrement cx by 1 jnz l1 ; jump to l1 if not zero mov fact,ax ; load ax content into fact hlt ; terminate the programcode ends ; code segment ends end start ; programs ends

Output:

Page 16 of 65

Page 17: mp lab

Fibonacci series

Page 17 of 65

Page 18: mp lab

data segment ; data segment starts list db 00h,01h ; defining a list in bytes count db 05h ; count is first operanddata ends ; data segment endsassume cs:code,ds:datacode segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax mov cl,count ; load cl register with count dec cl ; decrement cl by 1 mov si,offset list ; si is pointer, points list mov al,byte ptr[si] ; load al with [si] content inc si ; increment si by 1 add al,byte ptr[si] ; add al and [si] content inc si ; increment si by 1 mov byte ptr[si],al ; load [si] with al contentl1: dec si ; decrement si by 1 add al,byte ptr[si] ; add al and [si] content inc si ; increment si by 1 inc si ; increment si by 1 mov byte ptr[si],al ; load [si] with al content dec cl ; decrement cl by 1 jnz l1 ; jump to l1 if not zero hlt ; terminate programcode ends ; code segment ends end start ; program ends

Output:

Page 18 of 65

Page 19: mp lab

Greatest of three numbers

Page 19 of 65

Page 20: mp lab

data segment ; starts data segment list db 0ah,5h,0fh ; defining a list in bytes count db 02 ; first operand count big db 1 dup(0) ; big is operand to store the result small db 1 dup(0) ; small is operand to store the resultdata ends ; ends data segment assume cs:code,ds:datacode segment ; starts code segmentstart:mov ax,data ; initialize data segment mov ds,ax mov cl,count ; load cl with count mov si,offset list ; si is pointer, points list dec cl ; decrement cl by 1 l1:mov al,byte ptr[si] ; load al with [si] content inc si ; increment si by 1 cmp al,byte ptr[si] ; compare al content and [si] content jnc nocarry ; jump to nocarry if carry flag not set mov bl,al ; load bl with al content mov bh,byte ptr[si] ; load bh with [si] content jnz l2 ; jump to l2 if zero flag is not setnocarry:mov bl,byte ptr[si] ; load [si] content into bl mov bh,al ; load al content into bh l2:dec cl ; decrement cl jz l1 ; jump to l1 if zero flag set mov small,bl ; load bl content into small mov big,bh ; load bh content into big hlt ; terminate programcode ends ; ends code segment end start ; program ends

Output:

Page 20 of 65

Page 21: mp lab

Page 21 of 65

Page 22: mp lab

Addition of n-array elements

data segment ; data segment starts x db 05h ; x is first operand list db 01h,02h,03h,04h,05h ; defining a list of bytes sumcarry dw 1 dup(0) ; sumcarry is operand to store resultdata ends ; data segment endsassume cs:code,ds:datacode segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax xor ax,ax ; clear ax register content mov cl,x ; load cl with x mov si,offset list ; si is pointer, points list dec cl ; decrement cl by 1 mov al,byte ptr[si] ; load al with [si] content l1:inc si ; increment si by 1 add al,byte ptr[si] ; add al with [si] content jnz nocarry ; jump to nocarry ig not zero inc ah ; increment ah by 1nocarry:dec cl ; decrement cl by 1 jnz l1 ; jump to l1 if not zero mov sumcarry,ax ; load sumcarry with ax content hlt ; terminate progarmcode ends ; code segment ends end start ; program ends

Page 22 of 65

Page 23: mp lab

Output:

Page 23 of 65

Page 24: mp lab

Ascii addition

data segment ; data segment starts a db 09h ; first operand a b db 06h ; second operand b dv db 1 dup(0) ; dv is operand to store result asciiv dw 1 dup(0) ; asciiv is operand to store resultdata ends ; data segment endsassume cs:code,ds:data code segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax xor ax,ax ; clear ax register content mov al,a ; load al with a or al,30h ; or operation between al content and 30h mov bl,b ; load bl wiht b or bl,30h ; or operation between bl content and 30h add al,bl ; add al and bl contents aaa ; ascii adjust after addition mov bx,ax ; load bx with ax conten or bx,3030h ; or operation between bx content and 3030h mov asciiv, bx ; load asciiv with bx content ror ah,04h ; rotate of right the ah content by 4 times or al,ah ; or operation between al content and ah content mov dv,al ; load dv with al content hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 24 of 65

Page 25: mp lab

Output:

Page 25 of 65

Page 26: mp lab

Number of ones in a given number

data segment ; data segment starts num db 0fh ; num is first operand result dw 1 dup(0) ; result is opernad to store resultdata ends ; data segment endsassume cs:code,ds:datacode segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax xor ax,ax ; clear ax content mov cl,08h ; load cl with 08h mov al,num ; load al with num l2:rcr al,01h ; rotate right with carry by 1 jnc l1 ; jump to l1 if no carry inc bl ; increment bl by 1 l1:dec cl ; decrement cl by 1 jnz l2 ; jump to l2 if not zero mov result,bx ; load result with bx content hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 26 of 65

Page 27: mp lab

Output:

Page 27 of 65

Page 28: mp lab

.

Binary to gray code conversion

data segment ; data segment starts num db 00010010b ; num is first operand gcnum db 1 dup(0) ; gcnum is operand to store resultdata ends ; data segment endsassume cs:code,ds:data code segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax xor ax,ax ; clear ax register content mov al,num ; load al with num mov bl,num ; load bl with num ror bl,1 ; rotate right bl content by 1 xor al,bl ; xor operation between al and bl contents mov gcnum,al ; load gcnum with al content hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 28 of 65

Page 29: mp lab

Output:

Page 29 of 65

Page 30: mp lab

Gray to binary code conversion

data segment ; data segment starts gcnum db 1Bh ; gcnum is first operand num db 1 dup(0) ; num is operand to store resultdata ends ; data segment endsassume cs:code,ds:data code segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax xor ax,ax ; clear ax register content mov al,gcnum ; load al with gcnum mov bl,gcnum ; load bl with gcnum rol al,1 ; rotate left al by 1 and al,bl ; and operation between al and bl contents mov num,al ; load num with al content hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 30 of 65

Page 31: mp lab

Output:

Page 31 of 65

Page 32: mp lab

Packed numbers to unpacked numbers

data segment ; data segment starts packnum dw 0045h ; packnum is first operand unpacknum dw 1 dup(0) ; unpacknum is opernad to store result data ends ; data segment endsassume cs:code,ds:datacode segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax mov ax,packnum ; load ax with packnum and al,0Fh ; and operation al content and 0fH mov bl,al ; load bl with al mov ax,packnum ; load ax with packnum and al,0F0h ; and operation of al content 0f0h ror al,04h ; rotate right al content by 4 mov bh,al ; load bh with al content mov unpacknum,bx ; load unpacknum with bx content hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 32 of 65

Page 33: mp lab

Output:

Page 33 of 65

Page 34: mp lab

Unpacked to packed conversion

data segment ; data segment starts unpacknum dw 0405h ; unpacknum is first operand packnum dw 1 dup(0) ; packnum is operand to store resultdata ends ; data segment endsassume cs:code,ds:data code segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax mov ax,unpacknum ; load ax with unpacknum ror ah,4 ; rotate right ah by 4 add al,ah ; add al and ah contents xor ah,ah ; clear ah content mov packnum,ax ; load packnum with ax hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 34 of 65

Page 35: mp lab

Output:

Page 35 of 65

Page 36: mp lab

Arranging list of n numbers in ascending order

data segment ; data segment starts list db 33h,45h,47h,84h,31h ; defining a list of bytes count db 05h ; count is first operanddata ends ; data segment endsassume cs:code,ds:data code segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax mov cl,count ; load cl with count dec cl ; decrement cl by 1 l3:mov bl,cl ; load bl with cl content mov si,offset list ; si is pointer, points list l2:mov al,byte ptr[si] ; load al with [si] content inc si ; increment si by 1 cmp al,byte ptr[si] ; compare al and [si] contents jb l1 ; jump to l1 if borrow xchg al,byte ptr[si] ; exchange al and [si] contents dec si ; decrement si by 1 mov byte ptr[si],al ; load [si] with al content inc si ; increment si by 1 l1:dec bl ; decrement bl by 1 jnz l2 ; jump to l2 if not zero dec cl ; decrement cl by 1 jnz l3 ; jump to l3 if not zero hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 36 of 65

Page 37: mp lab

Output:

Page 37 of 65

Page 38: mp lab

Counting number even and odd numbers in a given list

data segment ; data segment starts list db 33h,45h,47h,84h,31h ; defining a list in bytes count db 05h ; count is first operand odd db 1 dup(0) ; odd is operand to store result even db 1 dup(0) ; even is operand to store resultdata ends ; data segment endsassume cs:code,ds:data code segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax xor bx,bx ; clear bx register content mov cl,count ; load cl with count mov si,offset list ; si is pointer, points list l2:mov al,byte ptr[si] ; load al with [si] content rcr al,1 ; rotate right with carry al by 1 jnc l1 ; jump to l1 if not carry inc bl ; increment bl by 1 l1:inc si ; increment si by 1 dec cl ; decrement cl by 1 jnz l2 ; jump to l2 if not zero mov odd,bl ; load odd with bl content mov al,count ; load al with count sub al,bl ; subtract bl from al content mov even,al ; load even with al content hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 38 of 65

Page 39: mp lab

Output:

Page 39 of 65

Page 40: mp lab

Multibyte addition

data segment ; data segment starts list1 db 33h,45h,97h,84h,31h ; defining list1 of bytes list2 db 45h,10h,15h,20h,35h ; defining list2 of bytes list3 db 5 dup(0) ; list3 is used to store result count db 05h ; coutn is first opernaddata ends ; data segment endsassume cs:code,ds:data code segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax xor bx,bx ; clear bx register content mov cl,count ; load cl with count mov si,offset list1 ; si is pointer, points list1 mov di,offset list2 ; di is pointer, points list2 mov bx,offset list3 ; bx is pointer, points list3 xor ax,ax ; clear ax register content l1:mov al,byte ptr[si] ; load al with [si] content add al,byte ptr[di] ; add al with [di] content mov byte ptr[bx],al ; load [bx] with al content inc si ; increment si by 1 inc di ; increment di by 1 inc bl ; increment bl by 1 dec cl ; decrement cl by 1 jnz l1 ; jump to l1 if not zero hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 40 of 65

Page 41: mp lab

Output:

Page 41 of 65

Page 42: mp lab

Counting number of signed and un signed numbers in a given list

data segment ; data segment starts list db 33h,45h,97h,84h,31h ; defining a list in bytes count db 05h ; count is first operand signed db 1 dup(0) ; signed is operand to store result unsigned db 1 dup(0); unsigned is operand to store resultdata ends ; data segment endsassume cs:code,ds:data code segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax xor bx,bx ; clear bx register content mov cl,count ; load cl with count mov si,offset list ; si is pointer, points list l2:mov al,byte ptr[si] ; load al with [si] content rcl al,01h ; rotate left with carry al by 1 jnc l1 ; jump to l1 if not carry inc bl ; increment bl by 1 l1:inc si ; increment si by 1 dec cl ; decrement cl by 1 jnz l2 ; jump to l2 if not zero mov signed,bl ; load signed with bl content mov al,count ; load al with count sub al,bl ; subtract bl from al content mov unsigned,al ; load unsigned with al content hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 42 of 65

Page 43: mp lab

Output:

Page 43 of 65

Page 44: mp lab

sum of n-square

data segment ; data segment starts n dw 05h ; n is first operand sum dw 1 dup(0) ; sum is operand to store resultdata ends ; data segment endsassume cs:code,ds:datacode segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax mov cx,n ; load cx with n l1:mov ax,cx ; load cx content into ax mul cx ; multiply with cx content add bx,ax ; add bx and ax contents dec cx ; decrement cx by 1 jnz l1 ; jump to l1 if not zero mov sum,bx ; load bx content into sum hlt ; terminate progarmcode ends ; code segment ends end start ; program ends

Page 44 of 65

Page 45: mp lab

Output:

Page 45 of 65

Page 46: mp lab

Matrix multiplication

data segment ; data segment starts list1 db 01h,02h,03h,04h,05h,06h,07h,08h,09h ; defining list1 of bytes list2 db 01h,00h,00h,00h,01h,00h,00h,00h,01h ; defining list2 of bytes list3 db 9 dup(0) ; list3 is used to store result count db 03h ; count is first operanddata ends ; data segment endsassume cs:code,ds:datacode segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax mov cl,count ; load cl with count mov dh,count ; load dh with count mov ch,09h ; load ch with 09h mov si,offset list1 ; si is pointer, points list1 mov di,offset list2 ; di is pointer, points list2 mov bx,offset list3 ; bx is pointer. points list3 l1:mov al,byte ptr[si] ; load al content to [si] mul byte ptr[di] ; multiply with [di] content add dl,al ; add dl and al contents inc si ; increment si by 1 dec ch ; decrement ch by 1 inc di ; increment di by 1 inc di ; increment di by 1 inc di ; increment di by 1 dec cl ; decrement cl by 1 jnz l1 ; jump to l1 if not zero mov byte ptr[bx],dl ; load [bx] with dl content inc bx ; increment bx by 1 xor dl,dl ; clear dl register content sub di,08h ; subtract 08h from di sub si,03h ; subtract 03h from si mov cl,count ; load cl with count cmp ch,01h ; compare ch with 01h jnc l1 ; jump to l1 if no carry dec dh ; decrement dh by 1 mov ch,09h ; load ch with 09h add si,03h ; add si with 03h sub di,03h ; subtract 03h from di cmp dh,01h ; compare dh and 01h jnc l1 ; jump to l1 if no carry hlt ; terminate the programcode ends ; code segment ends

Page 46 of 65

Page 47: mp lab

end start ; program ends

Output:

Page 47 of 65

Page 48: mp lab

Transpose of given matrix

data segment ; data segment starts list1 db 01h,02h,03h,04h,05h,06h,07h,08h,09h ; definging list1 of bytes list2 db 9 dup(0) ; list2 is used to store result count db 03h ; count is first operanddata ends ; data segment endsassume cs:code,ds:datacode segment ; code segment startsstart:mov ax,data ; initialize data segment mov ds,ax mov cl,count ; load cl with count mov bl,09h ; load bl with 09h mov si,offset list1 ; si is pointer, points list1 mov di,offset list2 ; di is pointer, points list2 l1:mov al,byte ptr[si] ; load al with [si] content mov byte ptr[di],al ; load [di] with al content inc si ; increment si by 1 dec bl ; decrement bl by 1 inc di ; increment di by 1 inc di ; increment di by 1 inc di ; increment di by 1 dec cl ; decrement cl by 1 jnz l1 ; jump to l1 if not zero sub di,08h ; subtract 08h form di mov cl,count ; load cl with count cmp bl,01h ; compare bl with 01h jnc l1 ; jump to l1 if no carry hlt ; terminate progarmcode ends ; code segment ends end start ; program ends

Page 48 of 65

Page 49: mp lab

Output:

Page 49 of 65

Page 50: mp lab

Comparision of two strings

data segment ; data segment starts str1 db "sri prakash",'$' ; str1 is first string count dw 000bh ; count is operand msg1 db "string are equal",'$' ; msg1 is string to display msg2 db "string are not equal",'$'; msg2 is string to display data ends ; data segment endsextra segment ; extra segment starts str2 db "sri prakash",'$' ; str2 is second string extra ends ; extra segment endscode segment ; code segment starts assume cs:code,ds:data,es:extrastart:mov ax,data ; initialize data segment mov ds,ax mov ax,extra ; initialize extra segment mov es,ax mov cx,count ; load cx with count mov si,offset str1 ; si is pointer, points str1 mov di,offset str2 ; di is pointer, points str2 l2:cld ; clear directional flag cmpsb ; compare ds:[si] with es:[di] and updates si and di jnz l1 ; jump to l1 if not zero dec cx ; decrement cx by 1 jnz l2 ; jump to l2 if not zero mov dx,offset msg1 ; dx is pointer, points msg1 mov ah,09h ; load ah with 09h int 21h ; interrup 21h is called jmp l3 ; jump to l3l1: mov dx,offset msg2 ; dx is pointer, points msg2 mov ah,09 ; load ah with 09 int 21h ; interrup 21h is calledl3: hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 50 of 65

Page 51: mp lab

Output:

Page 51 of 65

Page 52: mp lab

Page 52 of 65

Page 53: mp lab

Reverse of a string

extra segment ; extra segment starts str2 db 00bh ; str2 is string operand to store resultextra ends ; extra segment endsdata segment ; data segment starts str1 db "Balakrishna",'$'; str1 is string count dw 000bh ; count is first operand msg db "reverse string is:",'$'; msg is sting to be displayed data ends ; data segment endscode segment ; code segment starts assume cs:code,ds:data,es:extrastart:mov ax,data ; initialize data segment mov ds,ax mov ax,extra ; initialize extra segment mov es,ax mov si,offset str1 ; si is pointer, points str1 mov di,offset str2 ; di is pointer, points str2 mov cx,count ; load cx with count add di,cx ; add di with cx content dec di ; decrement di by 1 l1:cld ; clear directional flag lodsb ; load al with ds:[si] content and update si std ; set directional flag stosb ; store al content into es:[di] and update di dec cx ; decrement cx by 1 jnz l1 ; jump to l1 if not zero inc di ; increment di by 1 mov dx,offset msg ; dx is pointer, points msg mov ah,09h ; load ah with 09h int 21h ; interrupt 21h is called mov cx,count ; load cx with count l2:mov dl,byte ptr es:[di]; load dl with es:[di] content mov ah,02h ; load ah with 02h int 21h ; interrupt 21h is called inc di ; increment di by 1 dec cx ; decrement cx by 1 jnz l2 ; jump to l2 if not zero hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 53 of 65

Page 54: mp lab

Output:

Page 54 of 65

Page 55: mp lab

Page 55 of 65

Page 56: mp lab

String concatenation

data segment ; data segment starts str1 db "spcesri",'$'; str1 is string str2 db "prakash",'$'; str2 is string count1 dw 0007h ; count1 is first operand count2 dw 0007h ; count2 is second operand msg db "concatinated string is:",'$' ; msg is string to displaydata ends ; data segment endsextra segment ; extra segment starts cstr db 20 dup(0) ; cstr is string operand to store the resultextra ends ; extra segment endscode segment ; code segment starts assume cs:code,ds:data,es:extrastart:mov ax,data ; initialize data segment mov ds,ax mov ax,extra ; initialize extra segment mov es,ax xor ax,ax ; clear ax register xor cx,cx ; clear cx register mov cx,count1 ; load cx with count1 mov di,offset cstr ; di is pointer, points cstr l2:cld ; clear directional flag lodsb ; load al with ds:[si] content and update si cmp al,'$' ; compare al content with $ jz l1 ; jump to l1 if not zero stosb ; store al content into es:[di] and update di dec cx ; decrement cx by 1 jnc l2 ; jump to l2 if no carry l1:mov si,offset str2 ; si is pointer, points str2 mov cx,cx ; clear cx register mov cx,count2 ; load cx with coutn2 l4:cld ; clear directional flag lodsb ; load al with ds:[si] content and update si cmp al,'$' ; compare al content with $ jz l3 ; jump to l3 if zero stosb ; store al content into es:[di] and update di dec cx ; decrement cx by 1 jnc l4 ; jump to l4 if no carry l3:mov dx,offset msg ; dx is pointer, points msg mov ah,09h ; load ah with 09h int 21h ; interrupt 21h is called mov cx,000Eh ; load cx with 000Eh

Page 56 of 65

Page 57: mp lab

mov di,offset cstr ; di is pointer, points cstr l5:mov dl,byte ptr es:[di] ; load dl with es:[di] content mov ah,02h ; load ah with 02h int 21h ; interrupt 21h is called inc di ; increment di by 1 dec cx ; decrement cx by 1 jnz l5 ; jump to l5 if not zero hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 57 of 65

Page 58: mp lab

Output:

Page 58 of 65

Page 59: mp lab

Page 59 of 65

Page 60: mp lab

Finding the length of a given string

data segment ; data segment starts str1 db "krishna",'$'; str1 is sting count db 1 dup(0) ; count is first operand msg db "String length is:",'$' ; msg is string to displaydata ends ; data segment endscode segment ; code segment starts assume cs:code,ds:datastart:mov ax,data ; initialize data segment mov ds,ax xor cl,cl ; clear cl register content mov si,offset str1 ; si is pointer, points str1 l2:cld ; clear directional flag lodsb ; load al with ds:[si] content and update si cmp al,'$' ; compare al conternt with $ je l1 ; jump to l1 if equal inc cl ; increment cl by 1 jmp l2 ; jump to l2 l1:mov dx,offset msg ; dx is pointer, points msg mov ah,09h ; load ah with 09h int 21h ; interrupt 21h is called xor ah,ah ; clear ah register content mov al,cl ; load al with cl content mov bl,0Ah ; load bl with 0Ah div bl ; divide by bl content mov bx,ax ; load bx with ax content add bx,3030h ; add bx with 3030h mov ah,02h ; load ah with 02h mov dl,bl ; load dl with bl content int 21h ; interrupt 21h called mov dl,bh ; load dl with bh content int 21h ; interrupt 21h is called hlt ; terminate programcode ends ; code segment ends end start ; program ends

Page 60 of 65

Page 61: mp lab

Output:

Page 61 of 65

Page 62: mp lab

Led display

; this example shows how to access virtual ports (0 to 65535).; these ports are emulated in this file: c:\emu8086.io; this technology allows to make external add-on devices; for emu8086, such as led displays, robots, thermometers, stepper-motors, etc... etc...; anyone can create an animated virtual device.; c:\emu8086\devices\led_display.exe

#start=led_display.exe#

#make_bin#

name "led"mov ax, 1234out 199, axmov ax, -5678out 199, ax; Eternal loop to write; values to port:mov ax, 0x1: out 199, ax inc axjmp x1hlt

Page 62 of 65

Page 63: mp lab

Stepper motor

; this is an example of out instruction.; it writes values to virtual i/o port; that controls the stepper-motor.; c:\emu8086\devices\stepper_motor.exe is on port 7

#start=stepper_motor.exe#

name "stepper"

#make_bin#

steps_before_direction_change = 20h ; 32 (decimal)

jmp start

; ========= data ===============

; bin data for clock-wise; half-step rotation:datcw db 0000_0110b db 0000_0100b db 0000_0011b db 0000_0010b

; bin data for counter-clock-wise; half-step rotation:datccw db 0000_0011b db 0000_0001b db 0000_0110b db 0000_0010b

; bin data for clock-wise; full-step rotation:datcw_fs db 0000_0001b db 0000_0011b db 0000_0110b db 0000_0000b

; bin data for counter-clock-wise

Page 63 of 65

Page 64: mp lab

; full-step rotation:datccw_fs db 0000_0100b db 0000_0110b db 0000_0011b db 0000_0000b

start:mov bx, offset datcw ; start from clock-wise half-step.mov si, 0mov cx, 0 ; step counter

next_step:; motor sets top bit when it's ready to accept new commandwait: in al, 7 test al, 10000000b jz wait

mov al, [bx][si]out 7, al

inc si

cmp si, 4jb next_stepmov si, 0

inc cxcmp cx, steps_before_direction_changejb next_step

mov cx, 0add bx, 4 ; next bin data

cmp bx, offset datccw_fsjbe next_step

mov bx, offset datcw ; return to clock-wise half-step.

jmp next_step

Page 64 of 65

Page 65: mp lab

Traffic lights

; controlling external device with 8086 microprocessor.; realistic test for c:\emu8086\devices\Traffic_Lights.exe

#start=Traffic_Lights.exe#

name "traffic"

mov ax, all_redout 4, ax

mov si, offset situation

next:mov ax, [si]out 4, ax

; wait 5 seconds (5 million microseconds)mov cx, 4Ch ; 004C4B40h = 5,000,000mov dx, 4B40hmov ah, 86hint 15h

add si, 2 ; next situationcmp si, sit_endjb nextmov si, offset situationjmp next

; FEDC_BA98_7654_3210situation dw 0000_0011_0000_1100bs1 dw 0000_0110_1001_1010bs2 dw 0000_1000_0110_0001bs3 dw 0000_1000_0110_0001bs4 dw 0000_0100_1101_0011bsit_end = $

all_red equ 0000_0010_0100_1001b

Page 65 of 65