MASM1
MASM1
Programming
Machine-Level and Systems Programming
Dr. Rahul Raman
Mr Sukesh Babu V S
• 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:
Low level
High level Compiler language
Compiler Interpreter Assembler language
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$”
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
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.
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