EE3803 -- Lecture 6
A Useful Set of Assembly Language Instructions
EE3803-L04P01
Moving Data
Since assembly language programming is at such a low level, a large amount of time gets spent simply shuffling data, a byte or word at a time, between registers and memory. The mov instruction is the data movement workhorse and allows the following addressing modes: Destination, Source register, immediate register, register register, memory memory, immediate memory, register
Consider the following examples:
mov mov mov mov mov mov
ax, 1234h bx, ax [1234h], ax [1234h], 5678h cx,[0abcdh] cx, [bx]
;Move ;Move ;Move ;Move ;Move ;Move
the the the the the the
number 1234h into the ax register. contents of ax into the bx register. content of ax to memory address 1234h. number 5678h to memory address 1234h. content of memory address abcdh to cx. value pointed to by bx into cx.
EE3803-L06P02
Arithmetic and Logical Instructions
The most common arithmetic and logical instructions include operations such as AND, OR, XOR, ADD, and SUB. These instructions allow performing simple 8 or 16 bit mathematical operations on data and memory and may be used with the following addressing modes: Destination, Source register, immediate register, register register, memory memory, immediate memory, register
Consider the following examples:
add add add add add
ax, 1234h dx, si ax, [1234h] [1234h], dx bx, [bp]
;Adds 1234h to the number in ax. (ax = ax + 1234h) ;dx = dx + si ;ax = ax + (data located at DS:1234h) ;(data at DS:1234h) = (data at DS:1234h) + dx ;bx = bx + (data located at SS:BP)
EE3803-L06P03
Single Address Arithmetic Instructions
Another very common operation is to increment and decrement values in registers or in memory. The x86 provides two instructions INC and DEC to performe this operation as efficiently as possible. Another single operand instruction is NOT which does bitwise inversion on data. Since these instructions have only one operand, the choice of addressing is less than the other instructions: Source register memory
inc inc inc inc
ax dl [1234h] [bx]
;ax = ax + 1; ;dl = dl + 1 ;(data at DS:1234h) = (data at DS:1234h) + 1 ;(data at DS:BX) = (data at DS:BX) + 1
EE3803-L06P04
Performing Comparisons
Another extremely common operation in assembly language programs is comparison. In order to make a decision in a piece of code, two items need to be compared. This is done using the CMP instruction which allows the following addressing modes: Destination, Source register, immediate register, register register, memory memory, immediate memory, register
Consider the following examples:
cmp cmp cmp
ah, 0F1h dx, si ax, [1234h]
;Compare AH with the value F1h. ;Compare DX with SI. ;Compare ax with the data located at DS:1234h
EE3803-L06P05
What Happens During A Compare Operation?
CMP works by subtracting the source from the destination (but it does not change the content of either location). The result of this subtraction is stored in the
flag register of the
x86. This flag register contains (among others) the following entries: SF = Sign Flag ZF = Zero Flag CF = Carry Flag OF = Overflow Flag
Comparison gets a little tricky, since it matters if the numbers are being interpreted as
signed or unsigned.
Since the CMP instruction has no idea what kind of number is being
compared you have to provide the interpretation by choosing your branch (jump) instruction carefully!
For example, consider the following:
Condition (DEST) > (SRC) (DEST) < (SRC) (DEST) = (SRC)
Signed Numbers SF=OF, ZF=0 SFOF, ZF=0 ZF=1
Unsigned Numbers CF=0, ZF=0 CF=1, ZF=0 ZF=1
Thus, to get the correct jump, we need to interpret the numbers correctly!
EE3803-L06P06
Conditional (Short) Jumps
The signed/unsigned stuff effects your choice of a jump instruction. Thats why there are several different jumps that have exactly the same mnemonic! ALL conditional jumps have a very short jump range. You need to plan for this when you write your code. The conditional jumps are:
JC / JB / JNAE
- Jump on Carry / Below / Not Above or Equal
JE / JZ
- Jump on Equal / Zero
JNC / JAE / JNB
- Jump on No Carry / Above or Equal / Not Below
JNE / JNZ
- Jump on Not Equal / Not Zero
The syntax of all of these jumps is the same. For example:
JNE
exit
;Jump to lable exit if not equal.
Remember, exit must be located within 127 bytes of the jump instruction!
The unconditional Jump (JMP) does not have this restriction. An unconditional jump may be short (within 127 bytes), near (within a segment), or far (spanning segments). When using unconditional jumps, the compiler will choose the proper jump (in most cases).
EE3803-L06P06