KTMT-HN - Ch06.assembly Language Programming
KTMT-HN - Ch06.assembly Language Programming
Chapter 6
4
A typical instruction has 2 operands
- target operand (left)
- source operand (right)
3 kinds of operands exists
- immediate : value
- register : AX, EBX, DL, etc.
- memory location : variable or pointer
Examples:
mov ax, 2 mov [buffer], ax
target operand source operand target operand source operand
register immediate memory location register
mov reg8(16,32),reg8/mem8(16,32)
(copies content of register / memory location (source) to register (destination))
operands have to be
of the same size
Examples:
mov eax, 2334AAFFh mov [buffer], ax mov word [var], 2
reg32 imm32
mem16 reg16 mem16 imm16
Note that Asembler doesn’t remember the types of variables you declare, and so you must
explicitly code mov word [var], 2.
6
• <instruction> reg8/mem8(16,32),reg8/imm8(16,32)
(source - register / immediate, destination- register / memory location)
• <instruction> reg8(16,32),reg8/mem8(16,32)
(source - register / immediate, destination - register / memory location)
NEG – two’s complement negation - inverts all the bits, and adds 1
Example:
mov al, 11111110b
neg al ;(AL gets a value of not(11111110b)+1=00000001b+1=00000010b)
;(11111110b + 00000010b = 100000000b = 0)
<instruction> reg8/mem8(16,32),reg8/imm8(16,32)
(source - register / immediate, destination- register / memory location)
<instruction> reg8(16,32),reg8/mem8(16,32)
(source - register / immediate, destination - register / memory location)
AND– bitwise and – bit at index i of the destination gets ‘1’ if bits at
index i of both source and destination are ‘1’; otherwise ‘0’
Example:
or AL, BL ;(with same values of AL and BL as in previous example, AL gets a value 0)
CMP – Compare Instruction – compares integers
CMP performs a ‘mental’ subtraction - affects the flags as if the subtraction had
taken place, but does not store the result of the subtraction.
cmp reg8/mem8(16,32),reg8/imm8(16,32)
(source - register / immediate, destination- register / memory location)
cmp reg8(16,32),reg8/mem8(16,32)
(source - register / immediate, destination - register / memory location)
Examples:
mov al, 11111100b mov al, 11111100b
mov bl, 00000010b mov bl, 11111100 b
cmp al, bl ;(ZF (zero flag) gets a value 0) cmp al, bl ;(ZF (zero flag) gets a value 1)
JMP <Label>
JMP tells the processor that the next instruction to be executed is located
at the label that is given as part of jmp instruction.
Example:
this is infinite loop !
mov eax, 1
inc_again:
this instruction is never
inc eax
reached from this code
jmp inc_again
mov ebx, eax
J<Condition> <Label>
• execution is transferred to the target instruction only if the specified
condition is satisfied
• usually, the condition being tested is the result of the last arithmetic
or logic operation
mov eax, 1
Example: inc_again:
inc eax
mov eax, 1 cmp eax, 10
inc_again: jne inc_again ; if eax ! = 10, go back to loop
inc eax
cmp eax, 10
je end_of_loop ; if eax = = 10, jump to end_of_loop
jmp inc_again ; go back to loop
end_of_loop:
Instruction Description Flags
JO Jump if overflow OF = 1
JNO Jump if not overflow OF = 0
JS Jump if sign SF = 1
JNS Jump if not sign SF = 0
JE /JZ Jump if equal / Jump if zero ZF = 1
JNE/JNZ Jump if not equal / Jump if not zero ZF = 0
JB/JNAE/JC Jump if below / Jump if not above or equal / Jump if carry CF = 1
JNB/JAE/JNC Jump if not below /Jump if above or equal /Jump if not carry CF = 0
JBE / JNA Jump if below or equal / Jump if not above CF = 1 or ZF = 1
JA / JNBE Jump if above / Jump if not below or equal CF = 0 and ZF = 0
JL / JNGE Jump if less / Jump if not greater or equal SF <> OF
JGE / JNL Jump if greater or equal / Jump if not less SF = OF
JLE / JNG Jump if less or equal / Jump if not greater ZF = 1 or SF <> OF
JG / JNLE Jump if greater / Jump if not less or equal ZF = 0 and SF = OF
JP / JPE Jump if parity / Jump if parity even PF = 1
JNP / JPO Jump if not parity / Jump if parity odd PF = 0
JCXZ / JECXZ Jump if CX register is 0 / Jump if ECX register is 0 CX=0 / ECX=0
14
D<size> [ InitialValue] Pseudo-instruction <size> filed <size> value
DB byte 1 byte
DW word 2 bytes
DO octoword 16 bytes
Examples:
var: db 0x55 ; define a variable ‘var’ of size byte, initialized by 0x55
var: db 0x55,0x56,0x57 ; three bytes in succession
var: db 'a‘ ; character constant 0x61 (ascii code of ‘a’)
var: db 'hello',13,10,'$‘ ; string constant
var: dw 0x1234 ; 0x34 0x12
var: dw ‘A' ; 0x41 0x00 – complete to word
var: dw ‘AB‘ ; 0x41 0x42
var: dw ‘ABC' ; 0x41 0x42 0x43 0x00 – complete to word
var: dd 0x12345678 ; 0x78 0x56 0x34 0x12
• Any consecutive storage locations of the same size can be
called an array
X DW 40CH,10B,-13,0 ; Components of X are at X, X+2, X+4, X+6
Y DB 'This is an array' ; Components of Y are at Y, Y+1, …, Y+15
Z DD -2019, FFFFFh, 100b ; Components of Z are at Z, Z+4, Z+8
• Computational
R0 - R31
• Load/Store
• Jump and Branch
• Floating Point
• coprocessor
PC
• Memory Management HI
• Special LO
OP rs rt rd sa funct R format
OP rs rt immediate I format
op rs rt rd shamt funct
1010101010101010 0000000000000000
0000000000000000 1010101010101010
1010101010101010 1010101010101010
• MIPS has two basic data transfer instructions for
accessing memory
lw $t0, 4($s3) #load word from memory
sw $t0, 8($s3) #store word to memory
• The data is loaded into (lw) or stored from (sw) a
register in the register file – a 5 bit address
• The memory address – a 32 bit address – is formed by
adding the contents of the base address register to
the offset value
• A 16-bit field meaning access is limited to memory
locations within a region of 213 or 8,192 words (215 or
32,768 bytes) of the address in the base register
• Note that the offset can be positive or negative
• Load/Store Instruction Format (I format):
lw $t0, 24($s2)
op rs rt 16 bit offset
Memory
2410 + $s2 = 0xf f f f f f f f
op rs rt 16 bit offset
00
32
4
PC 32
• What if the branch destination is further away than can
be captured in 16 bits?
op 26 bit address
• Then can do procedure return with a
jr $ra #return
• Instruction format (R format):
op rs funct
Category Instr Op Code Example Meaning
Arithmetic add 0 and 32 add $s1, $s2, $s3 $s1 = $s2 + $s3
(R & I subtract 0 and 34 sub $s1, $s2, $s3 $s1 = $s2 - $s3
format) add immediate 8 addi $s1, $s2, 6 $s1 = $s2 + 6
or immediate 13 ori $s1, $s2, 6 $s1 = $s2 v 6
Data load word 35 lw $s1, 24($s2) $s1 = Memory($s2+24)
Transfer store word 43 sw $s1, 24($s2) Memory($s2+24) = $s1
(I format) load byte 32 lb $s1, 25($s2) $s1 = Memory($s2+25)
store byte 40 sb $s1, 25($s2) Memory($s2+25) = $s1
load upper imm 15 lui $s1, 6 $s1 = 6 * 216
Cond. br on equal 4 beq $s1, $s2, L if ($s1==$s2) go to L
Branch br on not equal 5 bne $s1, $s2, L if ($s1 !=$s2) go to L
(I & R
format) set on less than 0 and 42 slt $s1, $s2, $s3 if ($s2<$s3) $s1=1 else
$s1=0
set on less than 10 slti $s1, $s2, 6 if ($s2<6) $s1=1 else
immediate $s1=0
Uncond. jump 2 j 2500 go to 10000
Jump jump register 0 and 8 jr $t1 go to $t1
(J & R
format) jump and link 3 jal 2500 go to 10000; $ra=PC+4