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

MASM1

This document provides an introduction to MASM (Microsoft Macro Assembler) programming. It discusses different types of codes like machine codes, assembly codes, and high-level codes. It describes language translators like assemblers, compilers, and interpreters. It also describes assembly concepts like registers, segments, and the basic structure of a MASM program with examples.
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)
54 views

MASM1

This document provides an introduction to MASM (Microsoft Macro Assembler) programming. It discusses different types of codes like machine codes, assembly codes, and high-level codes. It describes language translators like assemblers, compilers, and interpreters. It also describes assembly concepts like registers, segments, and the basic structure of a MASM program with examples.
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/ 29

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


Types of Codes:
• Machine codes: Processor can understands only 0 and 1, ie, binary values,
since processor is purely an electronic device.
• Instructions and data's are stored in memory as bytes.
• Each instruction has two parts: numeric code and operands.

what operation to be OPCODE OPERAND on which data the


performed operation to be performed.
General format of an instruction
Example: Add A, B

• On x86 there is an instruction to add the content of EAX to the content of EBX and to
store the result back into EAX.
• This instruction is encoded (in hex) as: 03C3.
Dept of CSE, IIITDM 2
• Assembly codes:
• Each assembly instruction corresponds to exactly one machine instruction.
• The instruction (EAX = EAX + EBX) is written simply as:

• Assembler :An assembler translates assembly code into machine code.


• Assembly code is NOT portable across architectures.
• For different ISAs, different assembly languages are used.
• High Level Codes: High level programming language like C, C++, java etc are
used.( example : a=b+c;)

Dept of CSE, IIITDM 3


Language translators:
• Converts programing source code into language that the computer processor
can understand
Assembly
Translators language
High level
language Interpreter
Low level
language

Low level
High level Compiler language
Compiler Interpreter Assembler language

Assembly level Low level


language Assembler language

Dept of CSE, IIITDM 4


Types of assembler

• MASAM (Microsoft Macro Assembler ): DOS/Windows-based, produces 16-


bit/32-bit/64-bit output

• TASM (Borland Turbo Assembler ): DOS/Windows-based, produces 16-


bit/32-bit output

• NASM (Netwide Assembler ): produces 16-bit/32-bit/64-bit output

• All of those assemblers take the x86 instruction set as input

Dept of CSE, IIITDM 5


Assembly Memory Segments:
• Memory Segments : A segmented memory model divides the system memory
into groups of independent segments, referenced by pointers located in the
segment registers.
• Each segment is used to contain a specific type of data.
• Data segment
• to declare the memory region where data elements are stored for the program.
• Code segment
• This defines an area in memory that stores the instruction codes. This is also a
fixed area.
• Stack segment - this segment contains data values passed to functions and
procedures within the program.

Dept of CSE, IIITDM 6


Assembly Registers:
• To speed up the processor operations, the processor includes some internal
memory storage locations, called registers.
• The registers stores data elements for processing without having to access the
memory.
• A limited number of registers are built into the processor chip.
• Processor Registers
• General registers
• Data registers
• Pointer registers
• Index registers
• Control registers
• Segment register

Dept of CSE, IIITDM 7


Data Registers
• Four 32-bit data registers are used for arithmetic, logical and other operations.

• These 32-bit registers can be used in three ways:


• As complete 32-bit data registers: EAX, EBX, ECX, EDX.
• Lower halves of the 32-bit registers can be used as four 16-bit data registers: AX,
BX, CX and DX.
• Lower and higher halves of the four 16-bit registers can be used as eight 8-bit data
registers: AH, AL, BH, BL, CH, CL, DH, and DL.
Dept of CSE, IIITDM 8
Pointer Registers:
• Instruction Pointer (IP) - the 16-bit IP register stores the offset address of the
next instruction to be executed.
• Stack Pointer (SP) - the 16-bit SP register provides the offset value within the
program stack.
• Base Pointer (BP) - the 16-bit BP register mainly helps in referencing the
parameter variables passed to a subroutine.

Dept of CSE, IIITDM 9


Index Registers:
• Source Index (SI) - it is used as source index for string operations.
• Destination Index (DI) - it is used as destination index for string operations.

Dept of CSE, IIITDM 10


• Processor Registers
• General registers
• Data registers , Pointer registers, Index registers
• Control registers
• Segment register
Control Registers:
• The 32-bit instruction pointer register and 32-bit flags register combined are
considered as the control registers.
• The common flag bits are:
• Overflow Flag (OF): indicates the overflow of a high-order bit (leftmost bit) of
data after a signed arithmetic operation.
• Direction Flag (DF): determines left or right direction for moving or
comparing string data.
• When the DF value is 0, the string operation takes left-to-right direction and
when the value is set to 1, the string operation takes right-to-left direction.

Dept of CSE, IIITDM 11


• Interrupt Flag (IF): determines whether the external interrupts like, keyboard entry
etc. are to be ignored or processed.
• It disables the external interrupt when the value is 0 and enables interrupts when set to 1.
• Sign Flag (SF): shows the sign of the result of an arithmetic operation.
• A positive result clears the value of SF to 0 and negative result sets it to 1.
• Zero Flag (ZF): indicates the result of an arithmetic or comparison operation.
• A nonzero result clears the zero flag to 0, and a zero result sets it to 1.
• Auxiliary Carry Flag (AF): contains the carry from bit 3 to bit 4 following an
arithmetic operation.
• Parity Flag (PF): indicates the total number of 1-bits in the result obtained from an
arithmetic operation.
• Carry Flag (CF): contains the carry of 0 or 1 from a high-order bit (leftmost) after
an arithmetic operation

Dept of CSE, IIITDM 12


Segment Registers:
• Segments are specific areas defined in a program for containing data, code and
stack.
• There are three main segments:
• Code Segment: it contains all the instructions to be executed.
• A 16 - bit Code Segment register or CS register stores the starting address
of the code segment.
• Data Segment: it contains data, constants and work areas.
• A 16 - bit Data Segment register of DS register stores the starting address
of the data segment.
• Stack Segment: it contains data and return addresses of procedures or
subroutines.
• It is implemented as a 'stack' data structure.
• The Stack Segment register or SS register stores the starting address of the
stack

Dept of CSE, IIITDM 13


MASM Program Structure:
.8086
.model small
● .8086 limits the assembler to 8086 instruction set.
.stack 100h
.data ● While using model small, the program should

msg db “sample string $” contain only one data and code segment
.code • .stack 100h:This specifies the stack size for
mov ax, @data dynamic memory allocation needed by the program
… • .data: data segment
… msg db “sample string$” is used to create a byte
… and assign it the string value
end
• Code: code segment, contains the instructions
• End: end of the program
Dept of CSE, IIITDM 14
Comments:
• With MASM, comments are added after a ‘;’
• Example: add eax, ebx ;y=y+b
Variables
• There are various define directives to allocate space for variables for both
initialized and uninitialized data.
1. To allocate storage space to Initialized data:
Syntax:
variable-name define-directive initial-value
Example:
msg db “sample string$”

Dept of CSE, IIITDM 15


2. To allocate storage space to un-initialized data:

Allocated
Define Directive Description Define Directive Description
Space
RESB Reserve a Byte
DB Define Byte 1 byte
RESW Reserve a Word
DW Define Word 2 bytes
RESD Reserve a Doubleword
DD Define Doubleword 4 bytes
RESQ Reserve a Quadword
DQ Define Quadword 8 bytes
REST Reserve a Ten Bytes
DT Define Ten Bytes 10 bytes

Dept of CSE, IIITDM 16


Sample program in assembly
.model small .data • Reserve a byte of memory and
.stack 100h
msg db “hello initialize it with the string specified
.data
msg db “Hello world$” ; defining string world$” and give it a label ‘msg’.
.code • All memory initializations should
mov ax, @data ;initialize the data segment be done in the .data segment.
mov ds, ax • Any reference to a memory
initialization in the code segment
mov ah, 09h ;printing the message should be declared in the data
lea dx, msg segment to avoid errors.
int 21 h

mov ax, 4c00h ; exit from the program .code • To initialize the data segment.
int 21h mov ax, @data • This should be the first lines in the
end mov ds, ax code segment to use the data
segment, without this we cannot
access the data segment.

Dept of CSE, IIITDM 17


load effective address
Sample program in assembly
.model small • To print the string into standard
.stack 100h output.
.data • 09h is the DOS interrupt code to
msg db “Hello world$” ; defining string mov ax, 09h write a string to STDOUT.
.code lea dx, msg • Store this interrupt value in the
mov ax, @data ;initialize the data segment int 21h AX using mov instruction.
mov ds, ax • Then load string which is
labelled ‘msg’ into DX using lea.
mov ah, 09h ;printing the message • Call the ISRusing int instruction,
lea dx, msg
which will execute the interrupt
int 21 h
code stored in accumulator, which
mov ax, 4c00h ; exit from the program is 09h.
int 21h mov ax, 4c00h • To safely exit from the program.
end int 21h • 4c00h is the interrupt code to exit
from a program.

interrupt
Dept of CSE, IIITDM 18
Print two message
.model small
.stack 100h
.data
msg1 db “Good morning $” ; defining string
msg2 db “Welcome to IITDM$” ; defining string
.code
mov ax, @data ;initialize the data segment
mov ds, ax Output
mov ah, 09h ;printing the message
lea dx, msg1 Good morningWelcome to IIITDM
int 21 h

mov ah, 09h ;printing the message


lea dx, msg2
int 21 h
mov ax, 4c00h ; exit from the program
int 21h
end
Dept of CSE, IIITDM 19
.model small
.stack 100h
.data
msg1 db “Good morning $” ; mov ah, 09h ;printing the message
defining string lea dx, msg2
msg2 db “Welcome to IITDM$” ; int 21 h
defining string mov ax, 4c00h ; exit from the program
.code int 21h
mov ax, @data ;initialize the data segment end
mov ds, ax
mov ah, 09h ;printing the message
lea dx, msg1
Output
int 21 h
Good morning
mov dl,10 ;ascii 10-new line Welcome to IIITDM
mov ah,02h
int 21h

Dept of CSE, IIITDM 20


Program to add two numbers:

.model small mov ah, 09h


.stack 100h lea dx, msg1
.data int 21h
msg1 db "The sum is$"
.code mov dl,cl ; move the result to dl
mov ax, @data mov ah, 02h
mov ds,ax int 21h
mov al,03h
mov bl,04h mov ax, 4c00h
int 21h
add al, bl
end
add al,30h ; convert to ascii
mov cl, al ; mov result to cl
Output
The sum is7

Dept of CSE, IIITDM 21


.model small mov ah, 09h
.stack 100h lea dx, msg1 ; for printing the result or
.data int 21h ;message should be in dx or dl
msg1 db "The sum is$" mov dl,10
n1 db 05h mov ah, 02h
n2 db 04h int 21h
.code mov dl,cl ; move the result to dl
mov ax, @data mov ah, 02h
mov ds,ax int 21h
mov ax, 4c00h
mov al,n1
int 21h
mov bl,n2
end
add al, bl
add al,30h ; convert to ascii
Output
mov cl,al The sum is
7

Dept of CSE, IIITDM 22


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 this input data in ASCII form should be converted to binary for arithmetic calculations
and converted the result back to binary.
mov al, ‘3’ ; 3 is in asci form mov al, ‘3’ ; 3 is in asci form
sub al, 30h ; convert it into binary sub al, 30h ; convert it into binary
mov bl, 05h ; 05h is in hexa mov bl, ‘5’ ; convert it to binary
add al, bl sub bl, 30h
add al, 30h ; result is in binary, so add al, bl
; convert it into ascii for display add al, 30h ; result is in binary, so
; convert it into ascii for display

Dept of CSE, IIITDM 23


.model small
.stack 100h mov ah, 09h
.data lea dx, msg1
msg1 db "The sum is$" int 21h
.code mov dl,10
mov ax, @data mov ah, 02h
mov ds,ax int 21h
mov dl,cl ; move the result to dl
mov al,’3’ ;3 in ascii form
mov ah, 02h
sub al, 30h ; convert to binary
int 21h
mov bl,5h
add al, bl mov ax, 4c00h
add al,30h ; convert to ascii int 21h
mov cl, al end

Dept of CSE, IIITDM 24


add/sub 38h(hexa)
or
add/sub 48(decimal)
or
add/sub 60(octal)
or
Add/sub ‘0’(char)

Dept of CSE, IIITDM 25


Program to add two numbers from keyboard:
.model small add al,30h
.stack 100h mov cl,al
.data mov ah, 09h
msg1 db "The sum is$" lea dx, msg1 ; 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 ; for new line
int 21h
mov ah,1 ; for reading from keyboard
int 21h mov dl,cl ; move the result to dl
sub al, 30h ; convert to binary mov ah, 02h ; result printing
mov bl,al int 21h
mov ah,1 mov ax, 4c00h
int 21h int 21h
sub al, 30h end
add al, bl
Dept of CSE, IIITDM 26
Assembly system calls:

mov ah, 09h ; load the system call number 9 to ah


lea dx, msg ; load the EA of message in to dx Print the message
int 21 h ; call the interrupt

mov ah, 01h ; load the system call number 1 to ah


System read call int 21 h ; call the interrupt
; the entered value is in accumulator

mov ah, 02h ; load the system call number 1 to ah


int 21 h ; call the interrupt System write call
; the value in accumulator is displayed

Exit system call mov ax, 4c00h


int 21h

Dept of CSE, IIITDM 27


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 28


• 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 29

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy