1
Chapter -4&5
Basic Assembly Language
Comparisons
The CMP Instruction
The CMP instruction compares two operands. It is generally used in conditional execution. 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
CMP compares two numeric data fields. The destination operand could be either in register or in
memory. The source operand could be a constant (immediate) data, register or memory.
1
2
Example on comparisons
The loop instructions
Assembly Loops
The JMP instruction can be used for implementing loops. For example, the following code
snippet can be used for executing the loop-body 10 times.
MOV CL, 10
L1:
<LOOP-BODY>
DEC CL
JNZ L1
2
3
Loop format
Example on loop that display the integer number five 5 times
3
4
Example on JMP instruction
The processor instruction set however includes a group of loop instructions for implementing
iteration.
The basic LOOP instruction has the following syntax:
LOOP Destination(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.
The execution of the LOOP instruction involves two steps: First, it subtracts 1 from ECX.
4
5
Next, it compares ECX to zero. If ECX is not equal to zero, a jump is taken to the label identified
by destination. Otherwise, if ECX equals zero, no jump takes place, and control passes to the
instruction following the loop.
Assembly Procedures
Procedures or subroutines are very important in assembly language, as the assembly language
programs tend to be large in size. Procedures are identified by a name. Following this name, the
body of the procedure is described, which perform a well-defined job. End of the procedure is
indicated by a return statement.
Large problems can be divided into smaller tasks to make them more manageable
A procedure is the ASM equivalent of a Java or C++ function
PROC is a statement used to indicate the beginning of a procedure or subroutine.
ENDP indicates the end of the procedure.
5
6
Syntax:
Following is the syntax to define a procedure:
Procedure Name may be any valid identifier. Attribute is NEAR if the Procedure is in the same
code segment as the calling program; or FAR if in a different code segment.
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 as shown below:
CALL proc_name
The called procedure returns the control to the calling procedure by using the RET instruction.
When you create a procedure other than your program’s startup procedure, end it with a RET
instruction. RET forces the CPU to return to the location from where the procedure was called:
6
7
Example on PROC
7
8
8
9
9
10
10