Assemblylanguageprogrammingfundamentals 140313050423 Phpapp02
Assemblylanguageprogrammingfundamentals 140313050423 Phpapp02
Programming
Fundamentals
Example:
ADD2 PROC near ADD2 PROC FAR
ADD AX,BX ADD AX,BX
RET RET
ADD2 ENDP ADD2 ENDP
MACROS
• A macro is a group of repetitive instructions in
a program which are codified only once and
can be used as many times as necessary.
• Macro with in a macro is a nested MACRO
• A macro can be defined anywhere in program
using the directives MACRO and ENDM
• Syntax of macro:
Read MACRO
mov ah,01h
int 21h
ENDM
Display MACRO
mov dl,al
Mov ah,02h
int 21h
ENDM
Passing parameters to a macro
• Display MACRO MSG
mov dx, offset msg
mov ah,09h
Here parameter is
int 21h
MSG
ENDM
Calling macro:
DISPLAY MSG1
DISPLAY MSG2
MSG1 db “I am Fine $”
MSG2 db “Hello, How are you..? $”
Procedures Vs Macros
Procedures Macros
Accessed by CALL and RET mechanism Accessed by name given to macro when
during program execution defined during assembly
Machine code for instructions only put in Machine code generated for instructions
memory once each time called
Parameters are passed in registers, Parameters passed as part of statement
memory locations or stack which calls macro
Procedures uses stack Macro does not utilize stack
A procedure can be defined anywhere in A macro can be defined anywhere in
program using the directives PROC and program using the directives MACRO and
ENDP ENDM
Procedures takes huge memory for Length of code is very huge if macro’s are
CALL(3 bytes each time CALL is used) called for more number of times
instruction
Key common aspects for any
programming language
• Variables: declaration
• Assignment: assigning values to variable
• Input/output: displaying messages or
displaying variable values
• Control Flow: If Then, Loops
• Sub Programs: Definition , usage
Declaring Variables
• In c , java data types are used
• In Assembly language, Assembler Directives
are used(db , dw , dd , dq , dT)
• Example:
– reply db ‘y’ ( reply is a character variable)
– prompt db ‘Enter your favourite colour: ’, 0( prompt is a string terminated by
null)
– colour db 80 dup(?) (colour is an array of size 80)
– i db 20
– k db ?
– num dw 4000
– large dd 50000
• Indirect addressing
– Strings stored in memory
Message db ‘Hello’,0
8086 stack
• The stack is a block of memory that may be used for temporarily storing
the contents of registers inside CPU.
• Stack is accessed by using SP and SS.
• Stack is a Top Down Data Structure whose elements are accessed by using
a pointer (SP,SS).
• The stack is required when CALL instruction is used.
• Push
• Pop
• Top of stack
• Stack pointer
• LIFO
stack
• Example: Using the stack, swap the values of the ax and bx registers, so that ax
now contains what bx contained and bx contains what ax contained. (This is not
the most efficient way to exchange the contents of two variables). To carry out
this operation, we need at least one temporary variable:
Push ax
Mov ax,bx
popbx
Assignment
• For assigning values, MOV
• Mov ax,42
• Mov bx,45
So here MOV instruction carries out assignment
Syntax of MOV: MOV destination, source
ax = 2; mov ax, 2 ; ax = 2
if ( ax != bx ) sub ax, bx ; ax = 2 - bx
{ jz nextl ; jump if (ax-bx) == 0
ax = ax + 1 ; inc ax ; ax = ax + 1
} nextl:
bx = bx + 1 ; inc bx
Conditional jump instructions
If then
if ( i == 10 )
{
i=i+5;
j=j+5;
}
/* Rest of program */
If else
c = getchar() ;
if ( c == ‘A’ )
printf(“You guessed correctly !! “);
else
printf(“Sorry incorrect guess “) ;
• Upper case to lower case:
if ( c >= ‘A‘ && c <= ‘Z‘ ) or if ( c < ‘A‘ || c > ‘Z‘ )