Asm Lec#8
Asm Lec#8
13
Assembly Loops
T he JMP instruction can be used for implementing loops. For example, the following code snippet can be
MOV CL, 10
L1:
<LOOP-BODY>
DEC CL
JNZ L1
The processor instruction set however includes a group of loop instructions for implementing iteration. The basic
LOOP instruction has the following syntax:
LOOP 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 above code snippet could be written as:
mov ECX,10
l1:
<loop body>
loop l1
Example:
The following program prints the number 1 to 9 on the screen:
section .text
global main ;must be declared for using gcc
main: ;tell linker entry point
mov ecx,10
mov eax, '1'
l1:
mov [num], eax
mov eax, 4
mov ebx, 1
push ecx
TUTORIALS POINT
Simply Easy Learning
mov ecx, num
mov edx, 1
int 0x80
mov eax, [num]
sub eax, '0'
inc eax
add eax, '0'
pop ecx
loop l1
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .bss
num resb 1
When the above code is compiled and executed, it produces following result:
123456789
TUTORIALS POINT
Simply Easy Learning
CHAPTER
14
Assembly Numbers
N umerical data is generally represented in binary system. Arithmetic instructions operate on binary data.
When numbers are displayed on screen or entered from keyboard, they are in ASCII form.
So far, we have converted this input data in ASCII form to binary for arithmetic calculations and converted the
result back to binary. The following code shows this:
section .text
global main ;must be declared for using gcc
main: ;tell linker entry point
mov eax,'3'
sub eax, '0'
mov ebx, '4'
sub ebx, '0'
add eax, ebx
add eax, '0'
mov [sum], eax
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
nwln
mov ecx,sum
mov edx, 1
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db "The sum is:", 0xA,0xD
len equ $ - msg
segment .bss
sum resb 1
When the above code is compiled and executed, it produces following result:
TUTORIALS POINT
Simply Easy Learning
Such conversions are however, has an overhead and assembly language programming allows processing
numbers in a more efficient way, in the binary form. Decimal numbers can be represented in two forms:
ASCII form
ASCII Representation
In ASCII representation, decimal numbers are stored as string of ASCII characters. For example, the decimal
value 1234 is stored as:
31 32 33 34H
Where, 31H is ASCII value for 1, 32H is ASCII value for 2, and so on. There are the following four instructions for
processing numbers in ASCII representation:
AAA - ASCII Adjust After Addition
AAS - ASCII Adjust After Subtraction
AAM - ASCII Adjust After Multiplication
AAD - ASCII Adjust Before Division
These instructions do not take any operands and assumes the required operand to be in the AL register.
The following example uses the AAS instruction to demonstrate the concept:
section .text
global main ;must be declared for using gcc
main: ;tell linker entry point
sub ah, ah
mov al, '9'
sub al, '3'
aas
or al, 30h
mov [res], ax
section .data
msg db 'The Result is:',0xa
len equ $ - msg
section .bss
res resb 1
When the above code is compiled and executed, it produces following result:
TUTORIALS POINT
Simply Easy Learning
BCD Representation
There are two types of BCD representation:
In unpacked BCD representation, each byte stores the binary equivalent of a decimal digit. For example, the
number 1234 is stored as:
01 02 03 04H
The four ASCII adjust instructions, AAA, AAS, AAM and AAD can also be used with unpacked BCD
representation. In packed BCD representation, each digit is stored using four bits. Two decimal digits are packed
into a byte. For example, the number 1234 is stored as:
12 34H
Example:
The following program adds up two 5-digit decimal numbers and displays the sum. It uses the above concepts:
section .text
global main ;must be declared for using gcc
TUTORIALS POINT
Simply Easy Learning
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
section .data
msg db 'The Sum is:',0xa
len equ $ - msg
num1 db '12345'
num2 db '23456'
sum db ' '
When the above code is compiled and executed, it produces following result:
TUTORIALS POINT
Simply Easy Learning