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

Assembly Language Revision Notes

Uploaded by

2024sl70006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Assembly Language Revision Notes

Uploaded by

2024sl70006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT VII: Assembly Language Programming

TASM and MASM Assemblers


TASM (Turbo Assembler) and MASM (Microsoft Macro Assembler) are two popular
assembly language assemblers used in the development of low-level software for the
x86 family of processors.

1. TASM: TASM is a widely used assembler developed by Borland. It supports both 16-
bit and 32-bit assembly language programming for the x86 family of processors.
TASM is known for its ease of use, extensive documentation, and compatibility with a
wide range of operating systems. It has been used to develop a variety of software
applications, including device drivers, system utilities, and embedded systems.

An example program in TASM that prints "Hello" to the console

.MODEL SMALL

.STACK 100h

.DATA

message DB 'Hello$'

.CODE

MOV AH, 09h ; DOS function to print a string

MOV DX, OFFSET message ; Point DX to the message string

INT 21h ; Call the DOS interrupt

MOV AH, 4Ch ; DOS function to terminate the program

INT 21h ; Call the DOS interrupt


END

Explanation:

 The .MODEL SMALL directive tells TASM to generate a small memory model
program.
 The .STACK 100h directive reserves 256 bytes of memory for the stack.
 The .DATA section declares the message variable as a string containing the
characters "Hello" and a null terminator ($).
 The .CODE section is where the program code begins.
 The MOV AH, 09h instruction loads the value 9 into the AH register. This value
represents the DOS function to print a string to the console.
 The MOV DX, OFFSET message instruction loads the offset of the message
variable into the DX register. This register will point to the beginning of the string
to be printed.
 The INT 21h instruction calls the DOS interrupt to print the string to the console.
 The MOV AH, 4Ch instruction loads the value 4C into the AH register. This value
represents the DOS function to terminate the program.
 The INT 21h instruction calls the DOS interrupt to terminate the program and
return control to the operating system.
 The END directive marks the end of the program.

When the program is executed, it will print "Hello" to the console and then
terminate. The $ character at the end of the string is necessary to signal the end of
the string to the DOS interrupt that prints it.

2. MASM, on the other hand, is an assembler developed by Microsoft. Like TASM, it


supports both 16-bit and 32-bit assembly language programming for the x86 family
of processors. MASM is known for its powerful macro capabilities, which allow
programmers to define complex instructions using simple, reusable macros. It has
been used to develop a variety of software applications, including operating systems,
device drivers, and system utilities.

an example program in MASM that prints "Hello" to the console:


.model small

.stack 100h

.data

message db 'Hello', 0Ah, '$'

.code

mov ah, 09h ; DOS function to print a string

lea dx, message ; Load the offset of the message into DX

int 21h ; Call the DOS interrupt

mov ah, 4Ch ; DOS function to terminate the program

int 21h ; Call the DOS interrupt

end

Explanation:

 The .model small directive tells MASM to generate a small memory model
program.
 The .stack 100h directive reserves 256 bytes of memory for the stack.
 The .data section declares the message variable as a string containing the
characters "Hello" (0Ah is a line feed character for a new line) and a null
terminator ($).
 The .code section is where the program code begins.
 The mov ah, 09h instruction loads the value 9 into the ah register. This value
represents the DOS function to print a string to the console.
 The lea dx, message instruction loads the offset of the message variable into the
dx register using the "load effective address" instruction.
 The int 21h instruction calls the DOS interrupt to print the string to the console.
 The mov ah, 4Ch instruction loads the value 4C into the ah register. This value
represents the DOS function to terminate the program.
 The int 21h instruction calls the DOS interrupt to terminate the program and
return control to the operating system.
 The end directive marks the end of the program.
When the program is executed, it will print "Hello" to the console and then
terminate. The $ character at the end of the string is necessary to signal the end of
the string to the DOS interrupt that prints it.

Both TASM and MASM provide a powerful set of tools for low-level software
development. They allow programmers to write code that directly interacts with the
hardware, making them ideal for developing device drivers and system utilities.
However, assembly language programming can be complex and time-consuming, and it
requires a deep understanding of the underlying hardware architecture. Therefore, these
assemblers are typically used by experienced programmers with a background in low-
level programming.

In conclusion, TASM and MASM are two popular assembly language assemblers used in
the development of low-level software for the x86 family of processors. They provide a
powerful set of tools for low-level programming but require a deep understanding of
the underlying hardware architecture. These assemblers continue to be used today by
experienced programmers in the development of device drivers, system utilities, and
other low-level software applications.

Comparison between TASM and MASM assemblers


TASM (Turbo Assembler) and MASM (Microsoft Macro Assembler) are two popular
assemblers for the x86 architecture. While they are similar in many ways, there are
some key differences between them:

1. Syntax: TASM uses a different syntax than MASM. TASM uses an older syntax
that is based on the Intel syntax, while MASM uses a more modern syntax that is
based on the Microsoft syntax. This difference may affect the readability and
maintainability of the code.
2. Features: MASM has more features than TASM. For example, MASM supports
macros, which are reusable code segments that can be inserted into the program,
while TASM does not. MASM also supports more advanced data types, such as
structures and unions, which can make programming easier.
3. Compatibility: TASM is primarily used with Borland compilers, while MASM is
primarily used with Microsoft compilers. However, both assemblers can be used
with other compilers as well.
4. Performance: TASM is generally considered to be faster than MASM, but the
difference in performance is often negligible.
5. Support: MASM has better documentation and support from Microsoft, while
TASM has a smaller community of users and less support.

In conclusion, TASM and MASM are both popular assemblers for the x86
architecture. They provide a powerful set of tools for low-level programming and
have been used to develop a wide range of software applications. While TASM is
known for its ease of use and compatibility with a wide range of operating systems,
MASM is known for its powerful macro capabilities and ability to generate optimized
code.

Assembly instructions for Comparing & Branching,

In assembly language, comparing and branching instructions are used to compare


values and change the flow of execution based on the result of the comparison. Here
are some examples of comparing and branching instructions:

1. CMP - Compare The CMP instruction is used to compare two values. It subtracts
the second operand from the first operand and sets the flags based on the result,
without actually changing the values of the operands. The syntax for CMP is:

CMP dest, src

Here, dest and src are the operands to be compared. If dest is less than src, the
Carry flag (CF) is set. If dest is equal to src, the Zero flag (ZF) is set. If dest is
greater than src, the Sign flag (SF) is set.
2. JE/JZ - Jump if Equal/Jump if Zero The JE/JZ instruction is used to jump to a
different location in the code if the Zero flag (ZF) is set. The syntax for JE/JZ is:

JE/JZ label

Here, label is the destination address of the jump. If the Zero flag (ZF) is set, the
jump is taken.
3. JNE/JNZ - Jump if Not Equal/Jump if Not Zero The JNE/JNZ instruction is used to
jump to a different location in the code if the Zero flag (ZF) is not set. The syntax
for JNE/JNZ is:

JNE/JNZ label

Here, label is the destination address of the jump. If the Zero flag (ZF) is not set,
the jump is taken.
4. JA/JNBE - Jump if Above/Jump if Not Below or Equal The JA/JNBE instruction is
used to jump to a different location in the code if the Carry flag (CF) and Zero
flag (ZF) are both clear. The syntax for JA/JNBE is:

JA/JNBE label

Here, label is the destination address of the jump. If the Carry flag (CF) and Zero
flag (ZF) are both clear, the jump is taken.
5. JB/JC/JNAE - Jump if Below/Jump if Carry/Jump if Not Above or Equal The
JB/JC/JNAE instruction is used to jump to a different location in the code if the
Carry flag (CF) is set. The syntax for JB/JC/JNAE is:

JB/JC/JNAE label

Here, label is the destination address of the jump. If the Carry flag (CF) is set, the
jump is taken.
6. JG/JNLE - Jump if Greater/Jump if Not Less or Equal The JG/JNLE instruction is
used to jump to a different location in the code if the Zero flag (ZF) is clear and
the Sign flag (SF) is equal to the Overflow flag (OF). The syntax for JG/JNLE is:

JG/JNLE label
Here, label is the destination address of the jump. If the Zero flag (ZF) is clear and
the Sign flag (SF) is equal to the Overflow flag (OF), the jump is taken.

In conclusion, comparing and branching instructions are essential in assembly language


programming to compare values and change the flow of execution based on the result
of the comparison. These instructions are used to create conditional statements and
loops, making assembly language programs more powerful and flexible.

Numeric I/O, Macros, and Bit Operations


These are important aspects of assembly language programming. Here's a brief
overview of each:

1. Numeric I/O: Numeric I/O refers to the input and output of numerical data in
assembly language programs. The IN and OUT instructions are used to perform
input and output operations respectively. For example, the following code reads a
byte from port 0x60 and stores it in the AL register:

IN AL, 60h

Similarly, the following code outputs the value in the BL register to port 0x80 :

OUT 80h, BL

2. Macros: Macros are a way to define reusable code segments in assembly language.
They are similar to functions in high-level languages. Macros are defined using the
MACRO directive, and can take arguments. Here's an example of a simple macro that
adds two numbers:

MYMACRO MACRO arg1, arg2

MOV AX, arg1

ADD AX, arg2

ENDM
To use the macro, you would call it with two arguments:

MYMACRO 10, 20

This would expand to the following code:

MOV AX, 10

ADD AX, 20

3. Bit Operations: Bit operations are used to manipulate individual bits in a binary
number. Some common bit operations include AND, OR, XOR, NOT, SHIFT LEFT, and
SHIFT RIGHT. For example, the following code sets the 5th bit of the AL register to 1:

OR AL, 20h

This works by OR-ing the AL register with the value 0x20, which has a 1 in the 5th bit
position. Similarly, the following code clears the 3rd bit of the BL register:

AND BL, NOT 4h

This works by AND-ing the BL register with the bitwise NOT of the value 0x04, which
has a 0 in the 3rd bit position.
Overall, Numeric I/O, Macros, and Bit Operations are fundamental to low-level
programming and are used extensively in system-level programming and hardware
control.

You might also like