Assembly language program practice
In the previous article we have share many basic resources and other compile program with Assembly language. Do and keep practice with assembly helps to gather better knowledge on the language.
Example
section .data ;This statement for Data segment userMsg db 'Please enter a number: ' ; This is for the user to enter a number lenUserMsg equ $-userMsg ; This is the length of the message dispMsg db 'You have enter: ' lenDispMsg equ $-dispMsg section .bss ;This is Uninitialized data num resb 5 section .text ;This Code Segment global _start _start: ;This is for User prompt mov eax, 4 mov ebx, 1 mov ecx, userMsg mov edx, lenUserMsg int 80h ;Read and store the user input mov eax, 3 mov ebx, 2 mov ecx, num mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information int 80h ;Output the message 'The enter number is: ' mov eax, 4 mov ebx, 1 mov ecx, dispMsg mov edx, lenDispMsg int 80h ;Output the number enter mov eax, 4 mov ebx, 1 mov ecx, num mov edx, 5 int 80h ; Exit code mov eax, 1 mov ebx, 0 int 80h
OUTPUT:
Please enter a number:
1234
You have enter:1234
Most assembly language instructions require operands to be processed. An operand address provides the location, where the data to be processed is stored. Some instructions do not require an operand, whereas some other instructions may require one, two, or three operands. Generally, in assembly language an instruction needs two operands, the first operand is the destination, where data in a register or memory location and the second operand is the source. Source contains either the data to be delivered that is immediate addressing or the address that means in register / memory of the data. The source data remains unchanged after the operation.
The three basic modes of addressing are:
- Register addressing
- Immediate addressing
- Memory addressing
Register Addressing:
A register which contains the operand. In this addressing mode this is the first concern. In order to depending upon the instruction, the register may be the first operand, the second operand or both.
For example,
MOV DX, TAX_RATE ; This is the Register in first operand MOV COUNT, CX ; This is the Register in second operand MOV EAX, EBX ; This is Both the operands are in registers
As handing out data between registers does not comprise memory, it provides fastest processing of data.
Immediate Addressing:
In an immediate operand, it has a constant value or an expression. Therefore, when an instruction with two operands uses immediate addressing, the first operand may be a register or memory location, and the second operand is an immediate constant. The first operand defines the length of the data.
For example,
BYTE_VALUE DB 150 ; This is A byte value is defined WORD_VALUE DW 300 ; This statement A word value is defined ADD BYTE_VALUE, 65 ; This statement An immediate operand 65 is added MOV AX, 45H ; This statement is Immediate constant 45H is transferred to AX
Direct Memory Addressing:
This is used, when operands are specified in memory addressing mode. In direct access to main memory, usually to the data segment, it is needed. This is a way of addressing where results in slower processing of data. In order to locate the precise location of data in memory, we need the segment start address, which is typically found in the DS register and an offset value. Hence, this offset value is also called effective address. Again, In direct addressing mode, the offset value is definite directly as part of the instruction, indicated by the variable name. The assembler calculates the offset value and maintains a symbol table, which stores the offset values of all the variables used in the program. In direct memory addressing, one of the operands refers to a memory location and the other operand references a register.
For example,
ADD BYTE_VALUE, DL ; This statement is Adds the register in the memory location MOV BX, WORD_VALUE ; This statement is Operand from the memory is added to register
Direct-Offset Addressing
This addressing mode uses the arithmetic operators to modify an address. For instance, following definitions define tables of data;
A_BYTE_TABLE DB 14, 15, 22, 45 ; This statement is Tables of bytes A_WORD_TABLE DW 134, 345, 564, 123 ; This statement is Tables of words
The below operations access data from the tables in the memory into registers:
MOV CL, A_BYTE_TABLE [2] ; This is used to Gets the 3rd element of the BYTE_TABLE MOV CL, A_BYTE_TABLE + 2 ; This is used, Gets the 3rd element of the BYTE_TABLE MOV CX, A_WORD_TABLE[3] ; This is used, Gets the 4th element of the WORD_TABLE MOV CX, A_WORD_TABLE + 3 ; This is used , Gets the 4th element of the WORD_TABLE
Indirect Memory Addressing
This addressing mode operates the computer’s capability of Segment like Offset addressing. Normally, the base registers EBX, EBP or BX, BP and the index registers DI, SI, coded within square brackets for memory references, are used for this purpose. Indirect addressing is used for variables containing several elements like, arrays. Starting address of the array is stored in, say, the EBX register.
The following code snippet shows how to access different elements of the variable.
MINE_TABLE TIMES 10 DW 0 ; This is used to Allocates 10 words (2 bytes) each initialized to 0 MOV EBX, [MY_TABLE] ; This is used to Effective Address of MINE_TABLE in EBX MOV [EBX], 110 ; MINE _TABLE[0] = 110 ADD EBX, 2 ; EBX = EBX +2 MOV [EBX], 123 ; MINE _TABLE[1] = 123
The MOV Instruction
MOV instruction that is used for moving data from one storage space to another. The MOV instruction takes two operands.
- Syntax
The syntax of the MOV instruction is −
- MOV destination, source
The MOV instruction may have one of the following five forms, example statements are given below:
MOV register, register
MOV register, immediate
MOV memory, immediate
MOV register, memory
MOV memory, register
Here, Both of the operands in MOV operation should be of same size so the value of source operand remains unchanged. The MOV instruction reasons ambiguity at times. For example, look at the statements:
MOV EBX, [MINE_TABLE] ; Effective Address of MINE_TABLE in EBX
MOV [EBX], 110 ; MY_TABLE[0] = 110
It is not clear. Here, either you want to move a byte equivalent or you want a word equivalent of the number 110. In such cases, it is wise to use a type specifier. Here is a table shows some of the common type of specifiers:
Type Specifier Bytes addressed
BYTE 1
WORD 2
DWORD 4
QWORD 8
TBYTE 10
Example
There is a program which illustrates some of the concepts discussed in the post. It stores a name ‘ATM SAMSUZZAMAN’ in the data section of the memory, then changes its value to another name ‘Humayon Faridi’ programmatically and displays both the names.
section .text global _start ;This statement must be declared for linker (ld) _start: ; This tell linker entry point ;writing the name 'ATM SAMSUZZAMAN' mov edx,9 ;This is for message length mov ecx, name ; This is for message to write mov ebx,1 ; This is for a file descriptor (stdout) mov eax,4 ; This is system call number (sys_write) int 0x80 ; This call kernel mov [name], dword ' Humayon Faridi ' ; Changed the name to Humayon Faridi ;writing the name ‘Humayon Faridi’ mov edx,8 ;This for message length mov ecx,name ;This is for a message to write mov ebx,1 ;This is the file descriptor (stdout) mov eax,4 ;This is the system call number (sys_write) int 0x80 ;this call kernel mov eax,1 ;This is system call number (sys_exit) int 0x80 ;This call kernel section .data name db 'ATM SAMSUZZAMAN'
When the above code is compiled and executed, it produces the following result as:
ATM SAMSUZZAMAN Humayon Faridi