In assembly language, labels are used to mark locations in the code that can be referenced by jump instructions, such as jmp, jne, je, call, or ret. In MASM (Microsoft Macro Assembler), labels are used to identify where a procedure starts, where specific instructions or blocks of code are located, and where to jump during the program's execution.
In this explanation, I'll cover how labels work within procedures, how they can be used for conditional jumps, and demonstrate their functionality with examples.
A label in MASM is essentially a symbolic name that marks a specific address in the program. The label is followed by a colon (:), and it can be placed before any instruction or block of code. These labels allow you to reference locations in the code for things like loops, branching, or function calls.
; label_in_procedure.asm
.model small
.stack 100h
.data
msg db 'Hello, MASM!$', 0
.code
main:
mov ax, @data ; Initialize data segment
mov ds, ax
; Call the PrintMessage procedure
call PrintMessage
; Exit program
mov ah, 4Ch
int 21h
; Procedure to print a message
PrintMessage proc
; Start of label
start_printing:
mov ah, 09h ; DOS function to print a string
lea dx, msg ; Load address of msg into DX
int 21h ; DOS interrupt to print the string
ret ; Return from procedure
; End of procedure
PrintMessage endp
end main
start_printing is placed before the instruction mov ah, 09h. This label is not strictly necessary for this simple example, but it could be used in more complex scenarios (e.g., conditional jumps, loops).call PrintMessage instruction jumps to the PrintMessage procedure.ret instruction marks the end of the procedure, and control is returned to the calling program (main).In MASM, labels are often used in conjunction with conditional jump instructions (like jne, je, jg, jl, jmp, etc.) to alter the flow of control. This is useful when you want to perform different operations based on certain conditions within a procedure.
; conditional_jump_labels.asm
.model small
.stack 100h
.data
num1 dw 5 ; First number
num2 dw 10 ; Second number
result db 'Result is less than or equal to 10$', 0
greater_result db 'Result is greater than 10$', 0
.code
main:
mov ax, @data ; Initialize data segment
mov ds, ax
; Compare num1 and num2, and jump based on the result
call CompareNumbers
; Exit program
mov ah, 4Ch
int 21h
; Procedure to compare two numbers
CompareNumbers proc
; Compare num1 and num2
mov ax, num1 ; Load num1 into AX
cmp ax, num2 ; Compare AX (num1) with num2
; Jump based on comparison result
jg greater ; If AX > num2, jump to 'greater' label
jl less ; If AX < num2, jump to 'less' label
equal:
; This block is executed if num1 == num2
lea dx, result ; Load address of result message
mov ah, 09h ; DOS function to print string
int 21h
ret
greater:
; This block is executed if num1 > num2
lea dx, greater_result
mov ah, 09h
int 21h
ret
less:
; This block is executed if num1 < num2
lea dx, result
mov ah, 09h
int 21h
ret
CompareNumbers endp
end main
cmp ax, num2: Compares num1 (in AX) with num2.jg greater: If AX (the value of num1) is greater than num2, the control jumps to the greater label.jl less: If AX is less than num2, the control jumps to the less label.equal label is reached if num1 equals num2.greater_result or result) are displayed based on the comparison.Labels are also very useful for implementing loops, as they allow you to jump back to the beginning of a block of code. You can use labels with jmp or conditional jumps to create repetitive processes.
; loop_with_labels.asm
.model small
.stack 100h
.data
msg db 'Counting down: $', 0
num dw 5 ; Starting number
.code
main:
mov ax, @data ; Initialize data segment
mov ds, ax
; Print the message
lea dx, msg ; Load address of msg
mov ah, 09h ; DOS function to print string
int 21h
; Call the countdown procedure
call Countdown
; Exit program
mov ah, 4Ch
int 21h
; Procedure to perform countdown
Countdown proc
mov ax, num ; Load starting number
count_loop:
; Print the current value of AX (num)
; You would convert AX to ASCII and print here, but for simplicity, we assume it's done
; This could be extended to display each value of num in the message.
dec ax ; Decrement AX (count down)
cmp ax, 0 ; Compare AX to 0
jg count_loop ; Jump to count_loop if AX > 0
ret ; Return from procedure
Countdown endp
end main
count_loop label marks the beginning of the loop.dec ax instruction decrements the value of AX (which starts at num).cmp ax, 0 instruction checks if AX has reached 0.jg count_loop causes the program to jump back to the count_loop label if AX is still greater than 0.ret instruction returns from the Countdown procedure.Labels are integral in defining where procedures start and end. They can also be used to ensure that the program continues after a function call (via ret).
; function_call_labels.asm
.model small
.stack 100h
.data
msg db 'This is a simple function call.$', 0
.code
main:
mov ax, @data ; Initialize data segment
mov ds, ax
; Print the initial message
lea dx, msg ; Load the address of the message
mov ah, 09h ; DOS function to print string
int 21h ; Call DOS interrupt
; Call the MyFunction procedure
call MyFunction
; Exit program
mov ah, 4Ch
int 21h
; Procedure to print a message
MyFunction proc
lea dx, msg ; Load address of msg into DX
mov ah, 09h ; DOS function to print string
int 21h ; Print the string
ret ; Return to main
MyFunction endp
end main
MyFunction proc defines the procedure starting with the MyFunction label.ret ensures that the program returns control to the calling function (in this case, the main procedure).loop1, consider names that describe the purpose of the code at that location, such as start_loop or end_of_processing.jmp, jne, jg, jl, call, and ret.Open this section to load past papers