Showing posts with label ASM. Show all posts
Showing posts with label ASM. Show all posts

Steps of Assembly program writing

Bismillahir Rahmanir Rahim
Now it will be easy to understand how the code is writing  lets do writing
there are some steps to write a assembly:
a) Declare and define data
b) Declare and define stack
c) Declare and define instruction code
  • • Instruct the assembler to use a logical segment as physical segment
  • • Get the segment base number of data and stack segment in DS and SS
  • registers
  • • Write the data processing part of program
                • Copy data from memory into CPU registers
                • Do arithmetic / logic operation on data
               • Copy results from CPU registers to memory
  • • Call the Operating System to carry out input / output job (Optional)
  • • Call the Operating System to terminate the program
Example: Add two numbers 5 and  4 and display it on monitor
C/C++ program:
#include <stdlib.h>
void main()
{
int aa = 05, bb = 04, sum ;
sum = aa + bb ;
printf(”%d”, sum );
}


;; definition of logical data segment
datas SEGMENT
aa DB 05h
bb DB 04h
sum DB ?
datas ENDS
SEGMENT- ENDS : define a logical segment in
the memory. The starting memory location is given
some name, used for reference.
DB: (define byte) reserve a byte size memory ,
initialize it with a byte size number or leave it
uninitialized (?). The memory location may be
named and used as variable
;; definition of logical stack segment
stacks SEGMENT
DB 100 DUP(0)
stacks ENDS
n DUP(m): reserve ‘n’ byte size memory
locations, initialize it with a byte size number or
leave it uninitialized (?). The memory location is
not named
;; definition of logical code segment
codes SEGMENT
ASSUME CS: codes, DS: datas,
SS: stacks
start: ;; beginning address of program ({)
MOV ax, SEG datas
MOV ds, ax
MOV cx, SEG stacks
MOV ss, cx
;; data processing
MOV al, aa
MOV cl, bb
ADD al, cl
MOV sum, al
;; display result on monitor
ADD al, 30h
MOV dl, al
MOV ah, 02h
INT 21h
;; terminate program
MOV ax, 4C00h
INT 21h
codes ENDS
END start ;; end of program (})
ASSUME: declare which logical segment will be
used as physical segments – code, data and stack
segments
Initialization of Segments registers
When the operating system loads a program into
memory for execution
• The OS loads the CS segment register only with
segment base number.
• The DS, SS segment register is to be loaded with
segment base number by programmer.
SEG : load a register with segment base number.
• Data can be copied into segment registers
through general purpose register only
Operating system call for Input / output
• Convert the binary number into ASCII by adding
30h
• Put the data in DL reg
• Put function number = 02h to display character in
AH reg
• OS call to branch to subroutine, whose address is
in Interrupt vector table at location 0084h from
base of memory
System call for return to operating system
• Put function number = 4Ch in AH reg
• Put 00h in AL reg for normal termination
• OS call to branch to subroutine, whose address is
in Interrupt vector table at location 0084h from
base of memory
Reference:

                      Dr Ehteshamul Haque (lecture)
---------HAPPY CoDing---------

Some statement and segment to write Assembly program

Bismillahir Rahmanir Rahim
 Offset address (16bits): 
Displacement of a memory location from the segment base
address is called offset address
• Offset address is contained in pointer and index registers.
• The first byte in a segment has offset 0 and the last offset in a segment is FFFFh ,
determined by the size of pointer and index registers, which are 16bits long.
• The maximum length of a segment is 216 = 65536 bytes » 64Kb
• The maximum length of a program is 4* 216 = 262144 bytes » 256Kb
• Offset registers for
data segment are SI, DI, BX
stack segment are SP, BP
code segment is IP
• The offset address of memory location is defined by the programmer
Effective address: Association shown as REGsegment : REG pointer / index . or
XXXXh:YYYYh – where XXXX is the segment base number , YYYY is the offset
address.
• Segment base numbers are contained in segment registers
• DS for data segment
• SS for stack segment
• CS for code segment
• ES for extra data segment
Segment base number is same for all locations of a segment
• Association of segment and pointer / index registers
• Data segment : DS:SI or DS:DI or DS: BX
• Extra data segment : ES:DI or ES: BX
• Stack segment : SS:SP, SS:BP
• Code segment : CS:IP
Physical (Absolute) address: It is the 20bits address of a memory location.
• Absolute address is equal to segment base number contained in segment register
multiplied by 10h and adding to the offset contained in corresponding pointer or
index registers
Data segment DS*16d + SI
DS*16d + DI
DS*16d + BX
Extra data segment ES*16d + DI
ES*16d + BX
Stack segment SS*16d + SP
SS*16d + BP
Code segment CS*16d + IP
• Physical address is computed in the bus unit of CPU where the segment registers
and Instruction pointer registers are located.
Logical segments of an assembly program :
a) data segment – contains directives to reserve and initialize data variables
b) stack segment – contains directives to reserve and initialize memory locations to
keep return addresses and temporary variables
c) code segment – contains codes to carry out data processing
d) extra data segment - contains directives to reserve and initialize data variables, if
the data segment is full. This segment is optional
Type of assembly statements:
a) Directive: commands to assembler such as SEGMENT, ENDS, DB, DW, DD,
DUP(), ASSUME, SEG, OFFSET
b) Instruction: commands to CPU provided by instruction set such as MOV, ADD,
SUB, MUL, DIV, SHL, SHR, CMP, JMP, Jcondition
Format of assembly statements:
Label: statements ; comments
a) Format of assembly directives
Label: Directive ; comments
b) Format of assembly instruction
Label: Mnemonic Destination, Source ; comments
• Label – named memory location
• Mnemonic – name of operation codes (included in instruction set)
• Destination and source are the addresses of CPU and system memory registers
• CPU register addresses have names given in programming model
• Memory register addresses may be named by programmer or can be used by
its value

Definition of logical segment:

segName SEGMENT
- - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - -
segName ENDS

Assembly operations

Bismillahir Rahmanir Rahim
Can work upon maximum two operands (variables) only.
Format of instruction:
Label: opcode operand1(destination), operand2(source) ; comment
• Label – named location of instruction used as destination of branching,
• Opcode – name of operation, known as mnemonics or memory aid,
• Operand1 is also the destination of result and operand2 is the source of arithmetic
and logic operations,
• Operands may be registers, variables, or constants,
• The constant can appear only as the source of data processing,viz.,Operand2,
• For single operand instructions the source operand may be a register or variable,
• For two operands instructions the destination and source operands may be
  1. • register, constant
  2. • register, register
  3. • register, memory
  4. • memory, constant
  5. • memory, register
Example:
• mov, add, sub are the names of operations: assignment (=), addition (+) and
subtraction (-),
• mov al, aa results in al = aa,
• add al, bl results in al = al + bl,
• sub al, cl results in al = al - cl,

Category of instructions:
1.Data transfer: Between registers and memory MOV
Stack manipulation PUSH, POP
2.Arithmatic operations: ADD, SUB, MUL, DIV, SHR, SHL, INC, DEC
3.Logical operation: OR, AND, XOR, NOT, CMP
4.Branching: JMP , JCND, LOOP, CALL, RET
5.CPU control: INT, HLT, NOP
Special thanks to
Dr Ehteshamul Haque 

Save, Compile and Debugging a assembly program

Bismillahir Rahmanir Rahim
Hey guys, what's up?I'm coming back.Now I'm showing you How Can compile a assembly program.
Click here to collect  some necessary files and software.
Let's consider a very simple program.

youtube:

How can compile assembly program?



At first open a text editor or notepad and write your code example:


data segment
msg db "My first assembly program. $"
data ENDs
stack segment
db 100 Dup(0)
stack ENDs
code segment
Assume cs:code,ds:data,ss:stack
start:
 mov ax,seg data
 mov ds,ax
 mov ax,seg stack
 mov ss,ax

 mov dx,offset msg
 mov ah,09h
 int 21h

 mov ax,4c00h
 int 21h

code ENDs
End start

In notepad like as
Now click file>>save as

then file name: Your_program_name.ASM  (example: hello.asm)
Save as file type: All files
hit SAVE button. One thing remember your file location will be where your asm file are stored. otherwise you have to face compiling errors.
my program link is in my pc: E:\asm
Code writing  is done!
Now Compile the program.........
Here describe two way to compile....
Using cmd command:create a folder on D: drive. Name it ASM,
Place your assembly program file, MASM.EXE, LINK.EXE, MYLIB1.LIB in this folder,
           Assemble the program with MASM.EXE, linker LINK.EXE
At DOS prompt( or cmd ) type the following !
·         Create the translated file first.obj with assembler:
·         >masm first.asm <enter><enter><enter><enter>
·         Create the executable file first.exe with linker
·         >link first.obj <enter><enter><enter><enter><enter>
·         run the executable file:
>first <enter> 
The output will be 04 as expected.
Note the length of the file is only 321 bytes. 

  


using cmd command some tips: 
for jump one drive to another drive type Drive_name: hit Enter (example: E:)
for jump one folder to another folder type cd folder_name hit enter (Example cd asm)


 In the case of Windows 7 operating system we have to use the DOSBOX package to prepare and run the program.
a.       Create a folder on D: drive. Name it ASM,
b.      Place your assembly program file, MASM.EXE, LINK.EXE, MYLIB1.LIB in this folder,
c.       Double click on DOSBOX.EXE,
d.      Type-->: mount f  d:/asm (d:/asm is your program link in your pc)
e.       Type -->:  f:
f.       Run the commands in #b.
 #
·         Create the translated file first.obj with assembler:
·         >masm first.asm <enter><enter><enter><enter>
·         Create the executable file first.exe with linker
·         >link first.obj <enter><enter><enter><enter><enter>
·         run the executable file:
>first <enter> 
The output will be 04 as expected.
Note the length of the file is only 321 bytes.


  Debugging the program: stepwise execute each instructions and inspect the values in variables.


1.      Type DEBUG nameOfExecutivefile.exe
2.      Type -u to see the code and instruction of program
3.      Type -r to see the contents of the registers and next instruction
4.      Type -t to run an instruction at a time ( single stepping ):
5.      Type -p to run procedure  type INT XX
6.      Type –g to complete the program
7.      Type -q to exit DEBUG
 Reference:

                      Dr Ehteshamul Haque (lecture)
---------HAPPY CoDing---------

The ( + , - , * , / ) of two numbers in assembly using procedure.

Bismillahir Rahmanir Rahim
Solution:

if 1
include mylib1.lib
endif
mydata segment
am  db  "The sum is= $"
sm  db  "  The subtruction is = $"
mm  db  "  The multipliction is = $"
dm  db  "  The division part: $"
qm  db  "  The Quotient is = $"
rm  db  "  The reminder is= $"
aa  db  07 
ba  db  05
r1  db  ?
r2  db  ?
r3  db  ?
rq  db  ?
rr  db  ?

mydata ends
mystack segment
db 100 dup(0)
mystack ends
mycode segment
assume cs:mycode, ds:mydata, ss:mystack
start:
mov ax, seg mydata
mov ds, ax
mov ax, seg mystack
mov ss, ax

call sum

mov dx,offset am
mov ah,09
int 21h

mov bh,00
mov bl,r1
printd

call subb

mov dx,offset sm
mov ah,09
int 21h

mov bh,00
mov bl,r2
printd

call mult


mov dx,offset mm
mov ah,09
int 21h
mov bh,00
mov bl,r3
printd

call divv

mov dx,offset dm
mov ah,09
int 21h
mov dx,offset qm
mov ah,09
int 21h

mov bh,00
mov bl,rq
printd

mov dx,offset rm
mov ah,09
int 21h

mov bh,00
mov bl,rr
printd

mov ax, 4c00h
int 21h
printfd
sum PROC
mov al, aa
mov bl, ba
add al, bl
mov r1, al
 ret
sum ENDP

subb PROC
mov al, aa
mov bl, ba
sub al, bl
mov r2, al
 ret
subb ENDP

mult PROC
mov al, aa
mov bl, ba
mul bl
mov r3, al
 ret
mult ENDP

divv PROC
mov al, aa
mov bl, ba
mov ah,00
div bl
mov rq, al
mov rr,ah
 ret
divv ENDP

mycode ends
end start

The division of Two Numbers Using procedure in assembly.

Bismillahir Rahmanir Rahim
Solution:

if 1
include mylib1.lib
endif
data segment
qm  db  "The quation is = $"
rm  db  "   The reminder is= $"
aa  db  08
ba  db  05
rq  db  ?
rr  db  ?
data ends
stack segment
db 100 dup(0)
stack ends
code segment
assume cs:code, ds:data, ss:stack
start:
mov ax, seg data
mov ds, ax
mov ax, seg stack
mov ss, ax

call divv

mov dx,offset qm
mov ah,09
int 21h

mov bh,00
mov bl,rq
printd

mov dx,offset rm
mov ah,09
int 21h

mov bh,00
mov bl,rr
printd

mov ax, 4c00h
int 21h
printfd

divv PROC
mov al, aa
mov bl, ba
mov ah,00
div bl
mov rq, al
mov rr,ah
 ret
divv ENDP

code ends
end start

My first assembly program

Bismillahir Rahmanir Rahim
Solution:

data segment
msg db "My first assembly program. $"
data ENDs
stack segment
db 100 Dup(0)
stack ENDs
code segment
Assume cs:code,ds:data,ss:stack
start:
 mov ax,seg data
 mov ds,ax
 mov ax,seg stack
 mov ss,ax

 mov dx,offset msg
 mov ah,09h
 int 21h

 mov ax,4c00h
 int 21h

code ENDs
End start

Find the factorial using loop in assembly

Bismillahir Rahmanir Rahim
Solution: 

if 1
include mylib1.lib
endif
mydata segment
fact dd 01h 
num db ? 
mydata ends

mystack segment
db 100 dup(0)
mystack ends

mycode segment
assume cs:mycode, ds:mydata, ss:mystack
start:
mov ax, seg mydata
mov ds, ax
mov ax, seg mystack

mov ah,01h
int 21h; al = ascii
sub al,30h; al=bin
mov num, al
mov ah,00h
mov cx,ax
mov bx,01h
mov ax, word ptr fact ; ax = multiplicand
back: mul bx
 inc bx
 loop back
mov word ptr fact,ax
mov word ptr [fact+2],dx


mov bx,word ptr [fact+2]
printh
mov bx,word ptr fact
printh

 

mov ax, 4c00h
int 21h

printfh

mycode ends
end start

Addition of two digit numbers using procedure in Assembly

Bismillahir Rahmanir Rahim
Solution:

if1
includw mylib1.lib
ENDif
data segment
aa dw ?
ba dw ?
ca dw ?
sa dd ?
data ENDS
stack segment
db 100 Dup(0)
stack ENDS
code segment
Assume cs:code, ds:data, ss:stack
start:
 mov ax,seg data
 mov ds,ax
 mov ax,seg stack
 mov ss,ax

 call inum
 mov aa,ax
 call inum
 mov ba,ax
 call inum
 mov ca,ax

 mov ax,aa
 add ax,ba
 add ax,ca
 mov sa,ax
 mov bx,ax
 printd
 mov ah,4C00h
 int 21h

 inum proc
 
 mov ah,01h 
 int 21h
 sub al,30h
 push ax
 
 mov ah,01h
 int 21h
 sub al,30h
 push ax
 
 
 mov BP,SP
 mov dl,10
 mov ax,word ptr [bp+2]
 mul dl

 mov bx, word ptr [bp]
 add al,bl
 mov ah,00
 push ax
 mov ah,02
 mov dl,30h
 int 21h
 pop ax
 pop cx
 pop cx
 
  RET
 inum ENDP

 printfd
code ENDS
END start

The addition of array in assembly.

Bismillahir Rahmanir Rahim
Solution:

if 1
include mylib1.lib
endif
mydata segment
ar db 21,22,23,24,25
sum db 0
count EQU 5
mydata ends
mystack segment
db 100 dup(0)
mystack ends
mycode segment
assume cs:mycode, ds:mydata, ss:mystack
start:
mov ax, seg mydata
mov ds, ax
mov ax, seg mystack
mov ss, ax

mov cx,count
mov si, offset ar
mov al, sum

forr: add al,[si]
 inc si
 loop forr
endforr: mov sum,al

  mov bh,00
  mov bl,sum
  printd 

mov ax, 4c00h
int 21h

printfd

mycode ends
end start

The average of IUBAT in Assembly.

Bismillahir Rahmanir Rahim
Solution:

if 1
include mylib1.lib
endif
mydata segment
arr db 'IUBAT'
sum  db  ?
count EQu 5
mydata ends
mystack segment
db 100 dup(0)
mystack ends
mycode segment
assume cs:mycode, ds:mydata, ss:mystack
start:
mov ax, seg mydata
mov ds, ax
mov ax, seg mystack
mov ss, ax

mov cl,count
mov al,0
mov bx,0000
back: add al,[arr+bx]
inc bx
dec cl

jnz back
mov sum,al
 mov ah,00
mov cL,count
div cL
push ax
mov bh , 0
mov bl ,aL
printd 
pop ax
mov bh , 0
mov bl ,ah
printd 


mov ax, 4c00h
int 21h

printfd

mycode ends
end start

The sum of Characters in Assembly.

Bismillahir Rahmanir Rahim
Solution:

if 1
include mylib1.lib
endif
mydata segment
arr db 'IUBAT'
sum  db  ?
count EQu 5
mydata ends
mystack segment
db 100 dup(0)
mystack ends
mycode segment
assume cs:mycode, ds:mydata, ss:mystack
start:
mov ax, seg mydata
mov ds, ax
mov ax, seg mystack
mov ss, ax

mov cl,count
mov al,0
mov bx,0000
back: add al,[arr+bx]
inc bx
dec cl

jnz back
mov sum,al
 
mov bh , 0
mov bl ,sum
printd 

mov ax, 4c00h
int 21h

printfd

mycode ends
end start

The addition and subtraction of Arrays by using Assembly Language

Bismillahir Rahmanir Rahim
Here the first array is ar=[1,2,3,4,5]
2nd is br=[6,7,8,9,10]
3rd is cr=[11,12,13,14,15]
4th is dr=[16,17,18,19,20]
the equation is sr=ar+br+cr-dr.
I added a extra function to print string.
mov ah,09
mov dx,offset msg ;msg='The result are= '
int 21h

 
mov ah,09
mov dx,offset coma ;coma=' , '
int 21h
 I think, you understand it why i use this string. I am not use this two string the output will be show as
2d4d6d8d10d
instead of this The result is = 2d, 4d, 6d, 8d, 10d

solution:

if 1
include mylib1.lib
endif

mydata segment
aR db 1,2,3,4,5 
bR db 6,7,8,9,10 
cR db 11,12,13,14,15
dr db 16,17,18,19,20
sR db 0,0,0,0,0
msg db "The result are= $"
coma db " , $"
COUNT EQU 05
mydata ends

mystack segment
db 100 dup(0)
mystack ends

mycode segment
assume cs:mycode, ds:mydata, ss:mystack
start:
 mov ax, seg mydata
 mov ds, ax
 mov ax, seg mystack
 mov ss, ax

 MOV CX,COUNT
 MOV SI,0000

 mov ah,09
 mov dx,offset msg ;solved by adhoa
 int 21h
BACK: 
 mov al,[si+ar]
 add al,[si+br]
 add al,[si+cr]
 sub al,[si+dr]
 mov [si+sr], al
 inc si
 push cx
 mov bh,0
 mov bl,al

 printd
 pop cx 

 mov ah,09
 mov dx,offset coma ;solved by adhoa
 int 21h

 loop back

mov ax, 4c00h
int 21h

printfd

mycode ends
end start
if you have to face any problem to solve or understand it ,comment below or contact admain  in facebook  .
Best of luck.

subtraction of two numbers input from keybord via Assembly language

Bismillahir Rahmanir Rahim
At first we have to know  about some DOS function .
 Few of the functions of DOS interrupt 21h are described bellow : 
a. Function 01h : Keyboard input  Output: returns the ASCII code  of key pressed in AL       
   MOV AH, 01h
   INT   21h 
 b. Function 02h : Display a character,  whose ASCII code is in DL 
MOV AH, 02h
MOV DL, character  (character means variable name)
INT   21h  .
c. Function 09h : Print string, DS:DX  contains pointer to the character string  ending with ‘$’ or 24h (end of string marker)
 MOV AH, 09h
MOV DX,  OFFSET character
INT   21h 
d. Function 4Ch : Terminate current process   MOV AH, 4Ch and transfer controls to the invoking    MOV AL, 00h process. AL contains the return code    INT   21h

Solution:


datas      SEGMENT
aa      DB  00h   
bb DB  00h 
cc db  00h
first db "Enter your first number or Alphabat- $" 
secand db "Enter your secand number or Alphabat- $" 
datas ENDS 

staks      SEGMENT
DB         100 dup() 
staks      ENDS 

codes    SEGMENT                                          
ASSUME CS: codes, DS: datas,  SS: staks 
 Start:                                     
MOV        AX,  SEG  datas             
MOV        DS, AX              
MOV        CX,  SEG  staks             
MOV        SS, CX  

mov dx,offset first
mov ah,09
int 21h

MOV  AH, 01h ; function - input from keyboard 
INT 21h ; DOS call    ; ASCII of key pressed is in AL 
SUB al, 30h ; convert into binary 
MOV aa, al  

mov dx,offset secand
mov ah,09
int 21h

MOV  AH, 01h 
INT 21h 

SUB al, 30h 
MOV bb, al 
 
MOV  AL, aa 
MOV BL, bb 
CMP AL, BL ;AL > BL ? 
JBE ELSE_L ; No 
THEN:  ; Yes  
SUB  AL, BL  
MOV cc, AL  
JMP DISP_L 
ELSE_L:  
SUB  BL, AL  
MOV cc, BL 
DISP_L:  
MOV DL, cc  
ADD DL, 30h ; convert binary into ASCII 

MOV  AH, 02h ; call display character function 
INT 21h  ; OS call to display it             

MOV        AX, 4C00h             
INT          21h 
codes   ENDS            
 END START