Lesson 2
Lesson 2
Lesson 2
Lesson 2
Variables
• TASM provides as with different defined directives for reserving
storage space for variables. [DB, DW, DD, DQ]
• Syntax
[variable-name] define-directive initial-value [,initial-value]...
• Example
choice DB 'Y' ;ASCII of y = 79H
number1 DW 12345 ;12345D = 3039H
number2 DD 12345679 ;123456789D = 75BCD15H
Transfer Commands
• MOV
• LEA
• XCHG
MOV
• Syntax
MOV destination, source
• It copies the content of the source operand to the destination
• Source
Can be memory variable , register or immediate data
• Destination
Can be memory variable and register
• The size of the source and destination operand has to be equal.
• Moving the content of one memory variable to another is illegal.
• Moving the content of segment register to another segment register is also an
illegal move.
LEA
• Used to load the offset address of memory variable to registers
• Syntax
LEA reg , mem
• It loads the offset address of the memory variable into the register which is found in the
first operand.
• The first can’t be a memory variable and also the second one can’t be register.
• Ex.
Msg db “Hello”,’$’
.code
LEA DX, Msg ; loads the address of msg in dx.
• you can also use the mov operation with the offset keyword to load an offset address.
MOV DX, OFFSET Msg
XCHG
• Used to exchange or switch the contents of two operands
• Syntax
XCHG op1 , op2
• The operands can be memory variable , register or immediate data
Int 21h interrupt function
• 02, int 21h
• 09, int 21h
• 01, int 21h
• 3fh, int 21h
• Note: Whenever you want to access any code from int 21h, you have
to put the value in AH register and satisfy the pre-requirements
before calling the INT 21H function.
02, int 21h
• lets the user to display a single character or value.
• Pre-condition- store the value going to be displayed in DL register
• Steps
Move the 02 value to AH register
Put the value going to be displayed in DL register
Call the Int 21h function
• Ex.
Mov ah,02
Mov dl, ‘a’ ; ‘a’ is the character going to be displayed
Int 21h
• Post-condition
01, int 21h
• lets the user to insert a single character or value.
• Pre-condition- null
• Steps
Move the 01 value to AH register
Call the Int 21h function
• Notice:- after calling the Int 21h function you can access the input value from AL
register.
• Ex.
Mov ah,01
Int 21h
• Post-condition- the input value from the user is going to be stored in AL register
09, INT 21H
• lets the user to display a message or string.
• Pre-condition- define the message going to be displayed in the data segment,
load the starting address of the data segment in DS register, load the offset
address of the memory variable that contains the message in DX register.
• Steps
Move the 09 value to AH register
Load the offset address in DX register
Call the Int 21h function
• Post-condition- null
09, INT 21H
• Ex.
DATA
msg db “Hello”, ‘$’
CODE
mov ax, @data
mov ds, ax
mov ah,09
mov dx, offset msg ;equivalent to LEA dx, msg
Int 21h
3FH, INT 21H
• The 3Fh code in INT 21h interrupt function used to accept a string
from keyboard.
• Use of 3fh:
First you will move the code to ah.
Then you have to set the maximum character input in cx.
Next you will load the offset address for the input area.
Finally call the interrupt function
3FH, INT 21H
• Ex
Mov ah,3fh
Mov cx, 20 ; max char. 20
Lea dx, input
Int 21h
Structure
• .MODEL SMALL ; Defining the model you are going to use throughout your program
• .STACK 100 ; Defining your stack segment and its corresponding size
• .DATA ; A directive used to define your data segment
• ; your data definition goes here
• .CODE ; A directive used to define the code segment
• MAIN PROC ; Defining the main function/procedure where the execution starts
• ; your code goes here
• MOV AX, 4C00H ; An interrupt instruction which tells the assemble this is end of
• INT 21H processing
• MAIN ENDP ; A directive used to end the main procedure
• END MAIN ; A directive which tells the assembler that this is end of the program
• NB:-
• In every program you have to set your model before any instruction
• You can change the stack size based on your program
• You can leave the ‘.DATA’ directive if you don’t have any data definitions
• Any instruction written under the end of processing instruction is not going to be executed
Hello world
.model small
.stack 10
.data
str1 db "hello world$"
a dd 56781234h
.code
mov ax,@data
mov ds,ax
mov ah,09h
mov dx,offset str1
int 21h
mov ax,4c00h
int 21h
end
Personalized Greeting
• Modify the hello world program to accept the users name and display
a personalized hello program.