Part 4: Assembly language modes and code practices.

Assembly Language | 0 comments

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:

  1. Register addressing
  2. Immediate addressing
  3. 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

Chapter 4 Relational Algebra

Relational Algebra The part of mathematics in which letters and other general symbols are used to represent numbers and quantities in formula and equations. Ex: (x + y) · z = (x · z) + (y · z). The main application of relational algebra is providing a theoretical...

Chapter 3 Components of the Database System Environment

Components of the Database System Environment There are five major components in the database system environment and their interrelationships are. Hardware Software Data Users Procedures Hardware:  The hardware is the actual computer system used for keeping and...

Chapter 2: Database Languages and their information

Database Languages A DBMS must provide appropriate languages and interfaces for each category of users to express database queries and updates. Database Languages are used to create and maintain database on computer. There are large numbers of database languages like...

Database basic overview

What is DBMS? A Database Management System (DBMS) is a collection of interrelated data and a set of programs to access those data. Database management systems (DBMS) are computer software applications that interact with the user, other applications, and the database...

Laravel – Scopes (3 Easy Steps)

Scoping is one of the superpowers that eloquent grants to developers when querying a model. Scopes allow developers to add constraints to queries for a given model. In simple terms laravel scope is just a query, a query to make the code shorter and faster. We can...

CAMBRIDGE IELTS 17 TEST 3

READING PASSAGE 1: The thylacine Q1. carnivorous keywords: Looked like a dog had series of stripes ate, diet ate an entirely 1 .......................................... diet (2nd paragraph 3rd and 4th line) 1st and 2nd paragraph, 1st  paragraph,resemblance to a...

You may find interest following article

Chapter 4 Relational Algebra

Relational Algebra The part of mathematics in which letters and other general symbols are used to represent numbers and quantities in formula and equations. Ex: (x + y) · z = (x · z) + (y · z). The main application of relational algebra is providing a theoretical foundation for relational databases, particularly query languages for such databases. Relational algebra...

Chapter 3 Components of the Database System Environment

Components of the Database System Environment There are five major components in the database system environment and their interrelationships are. Hardware Software Data Users Procedures Hardware:  The hardware is the actual computer system used for keeping and accessing the database. Conventional DBMS hardware consists of secondary storage devices, usually...

Chapter 2: Database Languages and their information

Database Languages A DBMS must provide appropriate languages and interfaces for each category of users to express database queries and updates. Database Languages are used to create and maintain database on computer. There are large numbers of database languages like Oracle, MySQL, MS Access, dBase, FoxPro etc. Database Languages: Refers to the languages used to...

Database basic overview

What is DBMS? A Database Management System (DBMS) is a collection of interrelated data and a set of programs to access those data. Database management systems (DBMS) are computer software applications that interact with the user, other applications, and the database itself to capture and analyze data. Purpose of Database Systems The collection of data, usually...

Laravel – Scopes (3 Easy Steps)

Scoping is one of the superpowers that eloquent grants to developers when querying a model. Scopes allow developers to add constraints to queries for a given model. In simple terms laravel scope is just a query, a query to make the code shorter and faster. We can create custom query with relation or anything with scopes. In any admin project we need to get data...

CAMBRIDGE IELTS 17 TEST 3

READING PASSAGE 1: The thylacine Q1. carnivorous keywords: Looked like a dog had series of stripes ate, diet ate an entirely 1 .......................................... diet (2nd paragraph 3rd and 4th line) 1st and 2nd paragraph, 1st  paragraph,resemblance to a dog. … dark brown stripes over its back, beginning at the rear of the body and extending onto the...

CAMBRIDGE IELTS 17 TEST 4

PASSAGE 1 Q1 (False) (Many Madagascan forests are being destroyed by attacks from insects.) Madagascar's forests are being converted to agricultural land at a rate of one percent every year. Much of this destruction is fuelled by the cultivation of the country's main staple crop: rice. And a key reason for this destruction is that insect pests are destroying vast...

Cambridge IELTS 16 Test 4

Here we will discuss pros and cons of all the questions of the passage with step by step Solution included Tips and Strategies. Reading Passage 1 –Roman Tunnels IELTS Cambridge 16, Test 4, Academic Reading Module, Reading Passage 1 Questions 1-6. Label the diagrams below. The Persian Qanat Method 1. ………………………. to direct the tunnelingAnswer: posts – First...

Cambridge IELTS 16 Test 3

Reading Passage 1: Roman Shipbuilding and Navigation, Solution with Answer Key , Reading Passage 1: Roman Shipbuilding and Navigation IELTS Cambridge 16, Test 3, Academic Reading Module Cambridge IELTS 16, Test 3: Reading Passage 1 – Roman Shipbuilding and Navigation with Answer Key. Here we will discuss pros and cons of all the questions of the...

Cambridge IELTS 16 Test 2

Reading Passage 1: The White Horse of Uffington, Solution with Answer Key The White Horse of Uffington IELTS Cambridge 16, Test 2, Academic Reading Module, Reading Passage 1 Cambridge IELTS 16, Test 2: Reading Passage 1 – The White Horse of Uffington  with Answer Key. Here we will discuss pros and cons of all the questions of the passage with...

Cambridge IELTS 16 Test 1

Cambridge IELTS 16, Test 1, Reading Passage 1: Why We Need to Protect Bolar Bears, Solution with Answer Key Cambridge IELTS 16, Test 1: Reading Passage 1 – Why We Need to Protect Bolar Bears with Answer Key. Here we will discuss pros and cons of all the questions of the passage with step by step...

Cambridge IELTS 15 Reading Test 4 Answers

PASSAGE 1: THE RETURN OF THE HUARANGO QUESTIONS 1-5: COMPLETE THE NOTES BELOW. 1. Answer: water Key words:  access, deep, surface Paragraph 2 provides information on the role of the huarango tree: “it could reach deep water sources”. So the answer is ‘water’. access = reach Answer: water. 2. Answer: diet Key words: crucial,...

Cambridge IELTS 15 Reading Test 3 Answers

PASSAGE 1: HENRY MOORE (1898 – 1986 ) QUESTIONS 1-7: DO THE FOLLOWING STATEMENTS AGREE WITH THE INFORMATION GIVEN IN READING PASSAGE 1? 1. Answer: TRUE Key words: leaving school, Moore, did, father, wanted It is mentioned in the first paragraph that “After leaving school, Moore hoped to become a sculptor, but instead he complied with his father’s...

Cambridge IELTS 15 Reading Test 2 Answers 

PASSAGE 1: COULD URBAN ENGINEERS LEARN FROM DANCE ?  QUESTIONS 1- 6: READING PASSAGE 1 HAS SEVEN PARAGRAPHS, A-G. 1. Answer: B Key words: way of using dance, not proposing By using the skimming and scanning technique, we would find that before going into details about how engineers can learn from dance, the author first briefly mentions ways of...

Cambridge IELTS 15 Reading Test 1 Answers

PASSAGE 1: NUTMEG – A VALUABLE SPICE QUESTIONS 1- 4: COMPLETE THE NOTES BELOW.CHOOSE ONE WORD ONLY FROM THE PASSAGE FOR EACH ANSWER.WRITE YOUR ANSWER IN BOXES 1-8 ON YOUR ANSWER SHEET. 1. Answer: oval Key words: leaves, shape Using the scanning skill, we can see that the first paragraph describes the characteristics of the tree in detail, including...

CAMBRIDGE IELTS 14 READING TEST 4 ANSWERS 

PASSAGE 1: THE SECRET OF STAYING YOUNG QUESTIONS 1-8: COMPLETE THE NOTES BELOW. 1. ANSWER: FOUR / 4 Explain– Key words: focused age groups, ants– In paragraph 3, it is stated that “Giraldo focused on ants at four age ranges”,so the answer must be “four/4”. 2. ANSWER: YOUNG Explain– Key words: how well, ants, looked after– The first sentence of...

CAMBRIDGE IELTS 14 READING TEST 3 ANSWERS

PASSAGE 1: THE CONCEPT OF INTELLIGENCE QUESTIONS 1-3: READING PASSAGE 1 HAS SIX PARAGRAPHS, A-F. 1. ANSWER: B Explain ·     Key words: non-scientists, assumptions, intelligence, influence, behavior ·    People‟s behavior towards others‟ intelligence is mentioned in the first sentence of paragraph B: “implicit theories of...

CAMBRIDGE IELTS 14 READING TEST 2 ANSWERS

Cambridge IELTS 14 is the latest IELTS exam preparation.https://draftsbook.com/ will help you to answer all questions in cambridge ielts 14 reading test 2 with detail explanations. PASSAGE 1: ALEXANDER HENDERSON (1831-1913) QUESTIONS 1-8: DO THE FOLLOWING STATEMENTS AGREE WITH THE INFORMATION GIVEN IN READING PASSAGE 1? 1. ANSWER: FALSE Explain Henderson rarely...

Cambridge IELTS 14 Reading Test 1 Answers

Cambridge IELTS 14 is the latest IELTS exam preparation.https://draftsbook.com/ will help you to answer all questions in cambridge ielts 14 reading test 1 with detail explanations. PASSAGE 1: THE IMPORTANCE OF CHILDREN’S PLAY QUESTIONS 1-8: COMPLETE THE NOTES BELOW. 1. ANSWER: CREATIVITY Explain building a “magical kingdom” may help develop … – Key words: magical...

Cambridge IELTS 13 Reading Test 4 Answers 

PASSAGE 1: CUTTY SARK: THE FASTEST SAILING SHIP OF ALL TIME QUESTIONS 1-8: DO THE FOLLOWING STATEMENTS AGREE WITH THE INFORMATION GIVEN IN READING PASSAGE 1? 1. CLIPPERS WERE ORIGINALLY INTENDED TO BE USED AS PASSENGER SHIPS Key words: clippers, originally, passengerAt the beginning of paragraph 2, we find the statement: “The fastest commercial sailing...