0% found this document useful (0 votes)
14 views

MASM2

This document provides an introduction to addressing modes, instructions, and conditional jumps in MASM (Microsoft Assembly Language). It was authored by Dr. Rahul Raman and Mr. Sukesh Babu V S of the Department of Computer Science and Engineering at Indian Institute of Information Technology, Design and Manufacturing, Kancheepuram. The document explains different addressing modes, common instructions like MOV, ADD, SUB, MUL, DIV, logical instructions, and conditional jumps and their usage in MASM assembly language programming. It contains examples to demonstrate the syntax and use of various instructions.
Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
14 views

MASM2

This document provides an introduction to addressing modes, instructions, and conditional jumps in MASM (Microsoft Assembly Language). It was authored by Dr. Rahul Raman and Mr. Sukesh Babu V S of the Department of Computer Science and Engineering at Indian Institute of Information Technology, Design and Manufacturing, Kancheepuram. The document explains different addressing modes, common instructions like MOV, ADD, SUB, MUL, DIV, logical instructions, and conditional jumps and their usage in MASM assembly language programming. It contains examples to demonstrate the syntax and use of various instructions.
Copyright
© © All Rights Reserved
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/ 33

Introduction to MASM

Programming
Machine-Level and Systems Programming
Dr. Rahul Raman
Mr Sukesh Babu V S

INDIAN INSTITUTE OF INFORMATION TECHNOLOGY,


DESIGN AND MANUFACTURING
KANCHEEPRAM

Dept of CSE, IIITDM 1


Addressing Modes:
• The opcode field of an instruction specifies the operation to be performed.
• This operation must be executed on some data stored.
• The way the operands are chosen during program execution depends on
addressing modes.
• The different ways in which the location of an operand is specified in an
instruction is called as Addressing mode.

➢ Implied mode ➢ Direct addressing mode


➢ Immediate mode ➢ Indirect addressing mode
➢ Register mode ➢ Relative addressing mode
➢ Register indirect mode ➢ Indexed addressing mode
➢ Auto increment/decrement mode ➢ Base register addressing mode.

Dept of CSE, IIITDM 2


• The three basic modes of addressing are:
1. Register addressing
2. Immediate addressing
3. Memory addressing

mov ax, bx mov al, 03h mov al, 100


add al, bl add al, 04h add al, n1

Register addressing Immediate addressing Direct addressing

mov al, [100]


Indirect addressing

Dept of CSE, IIITDM 3


The MOV Instruction:
• Syntax of the MOV instruction is:
MOV destination, source
• The MOV instruction may have one of the following five forms:
MOV register, register
MOV register, immediate
MOV memory, immediate
MOV register, memory
MOV memory, register
The EQU Directive
The EQU directive is used for defining constants.
The syntax of the EQU directive is as follows:
CONSTANT_NAME EQU expression

Dept of CSE, IIITDM 4


.model small
.stack 100h
mov ah, 09h
.data
lea dx, msg ; for printing the result or message
msg db " the sum is $"
int 21h ; should be in dx or dl
num1 equ 05h
.code mov dl,10
mov ax, @data mov ah, 02h
mov ds,ax int 21h
mov al, num1
mov bl, 03h mov dl,cl ; move the result to dl
add al, bl mov ah, 02h
add al,30h int 21h
mov cl,al mov ax, 4c00h
int 21h
end

Dept of CSE, IIITDM 5


Arithmetic instruction:
The INC and DEC Instruction
SYNTAX:
INC destination DEC destination
mov al,03h mov bl, 05h
inc al dec bl

The ADD and SUB Instructions:


SYNTAX:
ADD/SUB destination, source

mov al,05h mov al,05h


mov bl,03h mov bl,03h
add al,bl sub al,bl

Dept of CSE, IIITDM 6


MUL/IMUL instruction:
MUL: unsigned
IMUL: signed
Syntax:
MUL/IMUL multiplier
mov al,03h
mov bl,02 h
mul bl
When two bytes are multiplied
• The multiplicand is in the AL register, and the multiplier is a byte in the memory or in
another register.
• The product is in AX. High order 8 bits of the product is stored in AH and the low
order 8 bits are stored in A.

Dept of CSE, IIITDM 7


When two one-word values are multiplied
• The resultant product is a double word, which will need two registers. The High order
(leftmost) portion gets stored in DX and the lower-order (rightmost) portion gets
stored in AX.

When two doubleword values are multiplied


• When two doubleword values are multiplied, the multiplicand should be in EAX and
the multiplier is a doubleword value stored in memory or in another register.
• The product generated is stored in the EDX:EAX registers, i.e., the high order 32 bits
gets stored in the EDX register and the low order 32-bits are stored in the EAX
register.

Dept of CSE, IIITDM 8


The DIV/IDIV Instructions:
• The division operation generates two elements - a quotient and a remainder.
• The DIV (Divide) instruction is used or unsigned data and the IDIV (Integer Divide) is
used for signed data.
SYNTAX: Mov ax, 10
DIV/IDIV divisor Mov bl, 2
Div bl
• The dividend is in an accumulator.
• Both the instructions can work with 8-bit, 16-bit or 32-bit operands.
• The operation affects all status flags.
When the divisor is 1 byte:
• The dividend is assumed to be in the AX register (16 bits).

Dept of CSE, IIITDM 9


When the divisor is 1 word :
• The dividend is assumed to be 32 bits long and in the DX:AX registers.
• The high order 16 bits are in DX and the low order 16 bits are in AX.
• After division, the 16 bit quotient goes to the AX register and the 16 bit remainder
goes to the DX register.

When the divisor is doubleword


• The dividend is assumed to be 64 bits long and in the EDX:EAX registers.
• The high order 32 bits are in EDX and the low order 32 bits are in EAX.
• After division, the 32 bit quotient goes to the EAX register and the 32 bit
remainder goes to the EDX register.

Dept of CSE, IIITDM 10


Logical Instructions:
• The processor instruction set provides the instructions AND, OR, XOR, TEST and NOT
Boolean logic which tests, sets and clears the bits according to the need of the program.

• These instructions compare or match bits of the operands and set the CF, OF, PF, SF and
ZF flags.

Dept of CSE, IIITDM 11


The AND Instruction:

• The AND instruction is used for supporting logical expressions by performing bitwise
AND operation.
• The bitwise AND operation returns 1, if the matching bits from both the operands are 1,
otherwise it returns 0.

• The AND operation can be used for clearing one or more bits.

Dept of CSE, IIITDM 12


The OR Instruction:
• The OR instruction is used for supporting logical expression by performing bitwise OR
operation.
• The bitwise OR operator returns 1, if the matching bits from either or both operands are
one.
• It returns 0, if both the bits are zero.

The XOR Instruction:


• The XOR operation sets the resultant bit to 1, if and only if the bits from the operands are
different.
• If the bits from the operands are same (both 0 or both 1), the resultant bit is cleared to 0.

XOR EAX, EAX; ?

Dept of CSE, IIITDM 13


The TEST Instruction:
• The TEST instruction works same as the AND operation, but unlike AND instruction, it
does not change the first operand.

TEST AL, 01H


The NOT Instruction:
• The NOT instruction implements the bitwise NOT operation.
• NOT operation reverses the bits in an operand.

Dept of CSE, IIITDM 14


Assembly Conditions:

The CMP Instruction:


• The CMP instruction compares two operands.
• This instruction basically subtracts one operand from the other for comparing whether the
operands are equal or not.
• It does not disturb the destination or source operands.
• It is used along with the conditional jump instruction for decision making.
SYNTAX
CMP destination, source

Dept of CSE, IIITDM 15


Unconditional Jump:
• The JMP instruction provides a label name where the flow of control is transferred
immediately.
SYNTAX
JMP label

Dept of CSE, IIITDM 16


Conditional Jump:
• If some specified condition is satisfied in conditional jump, the control flow is transferred
to a target instruction.
• Following are the conditional jump instructions used on signed data used for arithmetic
operations:

Dept of CSE, IIITDM 17


• Following are the conditional jump instructions used on unsigned data used for logical
operations:

Dept of CSE, IIITDM 18


• The following conditional jump instructions have special uses and check the value of
flags:

Dept of CSE, IIITDM 19


Syntax
J<Condition> set of instructions/label

Example:

Dept of CSE, IIITDM, KENCHEEPURAM 20


Largest of three numbers
.model small third:
.stack 100h cmp cl,[num3]
.data jg exit
msg db "The largest number is $" mov cl,[num3]
num1 db '4’ exit:
num2 db '7' mov [large],cl
num3 db '8' mov ah, 09h
large db 0 lea dx, msg ; for printing the result
.code int 21h ; should be in dx or dl
mov ax, @data mov dl,10
mov ds,ax mov ah, 02h
mov cl,[num1] int 21h
cmp cl,[num2] mov dl,large ; move the result to dl
jg third mov ah, 02h
mov cl,[num2] int 21h
cmp cl,[num3] mov ax, 4c00h
jg exit int 21h
mov cl,[num3] end
jmp exit

Dept of CSE, IIITDM 21


Assembly Loops:
• The JMP instruction can be used for implementing loops.

• The processor instruction set includes a group of loop instructions for implementing
iteration.
Syntax:
Loop Label
• Where, label is the target label that identifies the target instruction as in the jump
instructions.
• The LOOP instruction assumes that the ECX register contains the loop count.
• When the loop instruction is executed, the ECX register is decremented and the control
jumps to the target label, until the ECX register value, i.e., the counter reaches the value
zero.
Dept of CSE, IIITDM 22
Program to count from 0 to 9
.model small loop1:
.stack 100h add dl,30h ; to ascci for display
.data mov ah, 02h
msg db "The counting from 0 to 9 is $" int 21h
sub dl,30h ; to binary for increment
.code inc dl
mov ax, @data dec cx
mov ds,ax jnz loop1
mov ah, 09h
lea dx, msg ; for printing the result
int 21h ; should be in dx or dl mov ax, 4c00h
mov cx,10h int 21h
mov dl,0h end

Dept of CSE, IIITDM 23


Assembly Numbers:

• Numerical data is generally represented in decimal(internally binary) system.


• Arithmetic instructions operate on binary data.
• When numbers are displayed on screen or entered from keyboard, they are in ASCII form.
• So far, we have converted this input data in ASCII form to binary for arithmetic
calculations and converted the result back to binary.

mov eax,'3’
sub eax, ‘0’ ; convert ASCII form to binary
mov ebx, '4’
sub ebx, ‘0’ ; convert ASCII form to binary
add eax, ebx
add eax, ‘0’ ; convert binary to ASCII for display

Dept of CSE, IIITDM 24


• Assembly language programming allows processing numbers in a more efficient
way, in the binary form.
• Decimal numbers can be represented in two forms:
➢ASCII form
➢BCD or Binary Coded Decimal form
ASCII Representation
• In ASCII representation, decimal numbers are stored as string of ASCII characters.
• For example, the decimal value 1234 is stored as:
31 32 33 34H

• Where, 31H is ASCII value for 1, 32H is ASCII value for 2, and so on.
• There are four instructions for processing numbers in ASCII representation:
• AAA - ASCII Adjust After Addition • AAM - ASCII Adjust After Multiplication
• AAS - ASCII Adjust After Subtraction • AAD - ASCII Adjust Before Division
Dept of CSE, IIITDM 25
mov al, ‘9’ ; first operand
sub al,’0’ ; can be avoided
sub al, ‘3’ ; al=9-3
or al, 30h ; or al with 0
mov [res], ax

mov al, ‘9’ ; first operand


sub al, ‘3’ ; al=9-3
aas ; ASCII adjust after subtraction
or al, 30h ; or al with 0
mov [res], ax

Dept of CSE, IIITDM 26


BCD (Binary-Coded-Decimal)Representation(8421 code):
• Each decimal digit from 0 - 9 is replaced by its binary equivalent with 4 bit binary
number.

• Convert 0110100000111001(BCD) to its decimal equivalent.


• Divide the BCD number into four-bit groups and convert each to decimal.
• There are two types of BCD representation:
1. Unpacked BCD representation 2. Packed BCD representation
• In unpacked BCD representation, each byte stores the binary equivalent of a
decimal digit.
• For example, the number 1234 is stored as: Total 4 bytes are needed to represent the 4 digit
number 1234
01 02 03 04H
• The four ASCII adjust instructions, AAA, AAS, AAM and AAD can also be used
with unpacked BCD representation.
• In packed BCD representation, each digit is stored using four bits.
• Two decimal digits are packed into a byte. Total 2 bytes are needed to
• For example, the number 1234 is stored as: 12 34H represent the 4 digit number
1234
• There are two instructions for processing these numbers:
• DAA - Decimal Adjust After Addition ; DAS - Decimal Adjust After Subtraction
• There is no support for multiplication and division in packed BCD representation.
Dept of CSE, IIITDM 28
Assembly Arrays:

• The data definition directives can be used for defining a one dimensional array.

• The above definition declares an array of six words each initialized with the numbers 34,
45, 56, 67, 75, 89.
Multiple initializations
• The DUP assembler directive allows multiple initializations to the same value
Syntax:
marks DW 8 DUP (0)

Dept of CSE, IIITDM 29


.model small add bx,'0'
.stack 100h mov [sum], bx
.data mov ah, 09h
msg1 db "The sum is$" lea dx, msg1 ; for printing the result or
x db 2,2,3 message
sum dw '0' int 21h ; should be in dx or dl

.code mov dl,10


mov ax, @data mov ah, 02h
mov ds,ax int 21h
mov ax,3
mov bx,0 mov dx,sum ; move the result to dl
mov si,offset x mov ah, 02h
top: int 21h
add bx, [si]
add si,1 mov ax, 4c00h
dec ax int 21h
jnz top end

Dept of CSE, IIITDM 30


.model small add bx,'0'
.stack 100h mov [sum], bx
.data
msg1 db "The sum is$" mov ah, 09h
x db 3 dup (3) lea dx, msg1 ; for printing the result
sum dw '0' int 21h ; should be in dx or dl

.code mov dl,10


mov ax, @data mov ah, 02h
mov ds,ax int 21h
mov ax,3
mov bx,0 mov dx,sum ; move the result to dl
mov si,offset x mov ah, 02h
top: int 21h
add bx, [si]
add si,1 mov ax, 4c00h
dec ax int 21h
jnz top end

Dept of CSE, IIITDM 31


Assembly Procedures

• Procedures are identified by a name.


• Following this name, the body of the procedure is described.
• End of the procedure is indicated by a return statement.
Syntax to define a procedure:
proc_name:
procedure body
... Ret
• The procedure is called from another function by using the CALL instruction.
• The CALL instruction should have the name of the called procedure as argument.
CALL proc_name

• The called procedure returns the control to the calling procedure by using the RET
instruction.

Dept of CSE, IIITDM 32


.model small mov ah, 09h
.stack 100h lea dx, msg1 ; for printing the result or message
.data int 21h ; should be in dx or dl
msg1 db "The sum is$"
res dw '0' mov dl,10
mov ah, 02h
.code int 21h
mov ax, @data
mov ds,ax mov dx,res ; move the result to dl
mov cx,'3’ mov ah, 02h
sub cx,'0' int 21h
mov BX,'5’
sub bx,'0' sum:
clc mov ax,cx
call sum add ax, bx
mov [res], ax add ax,'0'
ret
Procedure named sum that adds the mov ax, 4c00h
variables stored in the CX and BX register int 21h
and returns the sum in the AX register. end
Dept of CSE, IIITDM 33

You might also like