You are on page 1of 37

CSC2201 Computer

Organization and Assembly


Language
Instructor Mr. Saqib Sadiq
Session Four Recap

• We talked about Instruction sets


• The Basic elements of Assembly language
• Flow chart and top down-structured design
Session Five
Lesson Plan

• x86 Addressing modes


• Data Definitions
• Symbolic Constants
• Data Transfer Instructions
x86 Addressing modes

• Program Addressing modes are various ways in which assembler allows us


to access our data.
• The simplest addressing mode is known as Immediate Addressing. In which
one of the operands must be a constant.
• Here is an example (NOTE: anything following a semi-colon is a comment):
mov ax,1 ;move 1 to the AX register
add ax,1 ;add 1 to the AX register
x86 Addressing modes
• Observe the following Assembly Language program:

• Variables declared under the .data directive are responsible for storing our data
• They are then operated on by the lines of code given under the .code directive
• This is a form of direct addressing in which we don’t make use of the integer constant (our data). Rather we store them as data in memory and
then access them from memory.

• Hence the second addressing mode we have examined is known as Direct Addressing. In
which the instruction is formed using a memory address. Please note that the memory
address is fixed.
x86 Addressing modes

• How can we modify this program to increase efficiency?


• Answer to this dilemma is to make use of registers.
• We can store the address of our data in registers.
• Thus, we can make use of the BX, BP, SI and DI registers. These high-speed memory
locations are designed to hold data addresses. This is known as Register Indirect
Addressing.
• Let’s take a look at our program now:
x86 Addressing modes

• Under the .data directive we used a label to refer to a directive initializer with an
initial and second value(line 05).
• Under the .code directive we had the base register point to the first number’s
address (line 09). The data located at this address was then moved to ax, notice the
brackets around BX (line 10).
• We moved BX forward by 2 bytes (16-bits) as we are using directive initializer of
DW (line 11). Which allowed us to add the second number to ax (line 12).
• To see this program in action let’s take a look at the log file.
x86 Addressing modes

• Is there anyway to increase the efficiency of our code? Can we have the
same results with fewer lines of code?
• The answer is yes!
• We will need to make use of Register Indirect + offset Addressing.
• In this type of program addressing mode we do not alter the contents of our
base register when we want to refer to the next consecutive memory space
• Let’s see how this works:
x86 Addressing modes

• Under our .data directive we used a label to refer to a directive initializer with an
initial and second value(line 05).
• Under our .code directive we had the base register point to the first number’s
address (line 09). The data located at this address was then moved to AX, notice
the brackets around BX (line 10).
• Instead of moving BX forward and altering its contents, we used an offset of 2. This
gives us the next memory location, whose data we can access through indirect
referencing. Once accessed we add it to ax (line 11).
• Let’s see this program in action.
x86 Addressing modes

BX has been loaded with the address of our data. The data itself has been loaded in to AX
x86 Addressing modes

BX has been offset by 2. This does not change its contents but still gives us the next address and its
data. Which can then be added to AX giving us our solution.
Session Five
Lesson Plan

• x86 Addressing modes


• Data Definitions
• Symbolic Constants
• Data Transfer Instructions
Data Definitions

• A data definition statement sets aside storage in memory for a variable,


with an optional name.
• A data definition has the following syntax:
Data Definitions

• This is an example of a data definition statement:


count DWORD 12345
• The optional name assigned to a variable must conform to the rules for
identifiers
• The directive in a data definition statement can be BYTE, WORD, DWORD
Data Definitions

• At least one initializer is required in a data definition, even if it is zero.


• Additional initializers, if any, are separated by commas.
• For integer data types, initializer is an integer constant or expression
matching the size of the variable’s type, such as BYTE or WORD.
Data Definitions

• If you prefer to leave the variable uninitialized (assigned a random value),


the ? symbol can be used as the initializer.
• All initializers, regardless of their format, are converted to binary data by
the assembler.
• Initializers such as 00110010b, 32h, and 50d all end up being having the
same binary value.
Session Five
Lesson Plan

• x86 Addressing modes


• Data Definitions
• Symbolic Constants
• Data Transfer Instructions
Symbolic Constants
• A good programming practice is to use symbols
• For example:
• CONST double PI = 3.14;

• Constants can be used to make code more readable


• In assembly we make use of EQU
• <symbol> EQU <value>
• PI EQU <3.14>
• What does EQU do?
• We can also use TEXTEQU
• continueMsg TEXTEQU <“Do you wish to continue (Y/N))?”>
• What does TEXTEQU do?
Symbolic Constants

• Alternatively constants can also be thought in other terms


• Such as integer constants
• A common example of an integer constant is the number ten
• Or string constant “Hello world!”
• Or even a character constant ‘A’
• In the context of our source code these elements are never changed
Session Five
Lesson Plan

• x86 Addressing modes


• Data Definitions
• Symbolic Constants
• Data Transfer Instructions
Data Transfer Instructions

• Previously we had discussed the MOV instruction. This is an example of a


data transfer instruction.
• We will now look at a more advanced version of the move instruction. It is
known as MOVZX
Data Transfer Instructions

• The MOVZX instruction (move with zero-extend) copies the contents of a


source operand into a destination operand and zero-extends the value to 16
• This instruction is only used with unsigned integers.
• Its first operand is the destination and is always a register.
• The second operand is the source.
Data Transfer Instructions

• The following example zero-extends binary 10001111 into AX:


Data Transfer Instructions
Data Transfer Instructions

• The MOVSX instruction (move with sign-extend) copies the contents of a


source operand into a destination operand and sign-extends the value to 16.
• This instruction is only used with signed integers.
Data Transfer Instructions

• An operand is sign-extended by taking the smaller operand’s highest bit and


repeating (replicating) the bit throughout the extended bits in the
destination operand.
Data Transfer Instructions

• The following example sign-extends binary 10001111b into AX:


Data Transfer Instructions

• The following example sign-extends binary 10001111b into AX:


Data Transfer Instructions
• The following figure demonstrates this concept
Data Transfer Instructions

• Unfortunately, both the MOVZX/MOVSX instruction do not exist on the


8086. They were added in the 80386.
• Therefore I can’t show you a demo with our current emulator.
• However our next data transfer instruction can be demonstrated.
Data Transfer Instructions

• The XCHG (exchange data) instruction exchanges the contents of two


operands.
• The rules for operands in the XCHG instruction are the same as those for the
MOV instruction, except that XCHG does not accept immediate operands.
• This instruction is great for array sorting applications.
Data Transfer Instructions

• Here is a simple program that demonstrates XCHG.


Data Transfer Instructions

• If I were to execute the first two instructions my registers will look like this:
Data Transfer Instructions

• Finally when I run my XCHG instruction, my registers will look like this:
Summary

• Today we talked about the x86 Addressing modes, Data Definitions,


Symbolic Constants and Data Transfer Instructions
Today’s practical

• Using the log we reviewed and your lecture notes. Create a program that
adds three 16-bit integers using Register Indirect Addressing.
• Modify your program to do the same using Register Indirect + Offset
Addressing.
Thanks

• Any questions?
• Feel free to reach out
• saqib.sadiq@szabist.pk

You might also like