Computer Architecture Lab
Computer Architecture Lab
Assembly Language
Practical Session 1
Data Representation Basics
• Bit – basic information unit: (1/0)
232-1
• Main Memory is an array of bytes,
…
addressed by 0 to 232-1=0xFFFFFFFF
2K-1
232 bytes = 4∙210∙3 bytes = 4 G bytes
address …
space
1 physical
memory
0
Registers
Register file - CPU unit which contains (32 bit) registers.
index registers
ESP, EBP, ESI, EDI
(Stack pointer - contains the address of last used
dword in the stack, Base pointer, Source index,
Destination Index)
Note that the list of registers above is partial. The full list can be found here.
Assembly Language Program
• consists of a series of processor instructions, meta-statements, comments, and
data
• translated by assembler into machine language instructions (binary code) that
can be loaded into memory and executed
• NASM - Netwide Assembler - is assembler and for x86 architecture
Example:
assembly code:
MOV AL, 61h ; load AL with 97 decimal (61 hex)
binary code:
10110000 01100001
Examples:
mov ax, 2 ; moves constant 2 to the register ax
buffer: resb 64 ; reserves 64 bytes
Notes:
- backslash (\) uses as the line continuation character: if a line ends with backslash, the next line is
considered to be a part of the backslash-ended line
- no restrictions on white space within a line
- a colon after a label is optional
Instruction Arguments
A typical instruction has 2 operands
- target operand (left)
- source operand (right)
Examples:
mov ax, 2 mov [buffer], ax
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, 0x2334AAFF mov [buffer], ax mov word [var], 2
reg32 imm32
mem16 reg16 mem16 imm16
Note that NASM doesn’t remember the types of variables you declare . It will deliberately remember nothing
about the symbol var except where it begins, and so you must explicitly code mov word [var], 2.
Basic Arithmetical Instruction
[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)
[instruction] reg8/mem8(16,32)
(source / 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)
Basic Logical Instructions
[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 11000000)
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)
Label – specifies instruction’s offset (address)
• each instruction / data has its offset (address)
• if we want to refer to the specific instruction / data in the code, we
should mark it with a label
• (non-local) labels have to be unique
• an instruction that follows a label can be at the same / next line
• colon is optional
Examples:
my_instruction: add ax, ax ;(my_instruction is an address of ‘add ax, ax’ instruction)
buffer: db 0
add byte [buffer], 2 ;([buffer] gets a value of [buffer]+2)
JMP – unconditional jump
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> – conditional jump
j<cond> label
Example:
read_char:
… ; get a character into AL
cmp al, ‘a’ ; compare the character to ‘a’
je a_received ; if value of al register equals to ‘a’, jump to a_received
jmp read_char ; go back to read another
a_received:
…
Jcc: Conditional Branch
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 Jump if equal ZF = 1
JZ Jump if zero
JNE Jump if not equal ZF = 0
JNZ Jump if not zero
JB Jump if below CF = 1
JNAE Jump if not above or equal
JC Jump if carry
JNB Jump if not below CF = 0
JAE Jump if above or equal
JNC Jump if not carry
JBE Jump if below or equal CF = 1 or ZF = 1
JNA Jump if not above
JA Jump if above CF = 0 and ZF = 0
JNBE Jump if not below or equal
JL Jump if less SF <> OF
JNGE Jump if not greater or equal
JGE Jump if greater or equal SF = OF
JNL Jump if not less
JLE Jump if less or equal ZF = 1 or SF <> OF
JNG Jump if not greater
JG Jump if greater ZF = 0 and SF = OF
JNLE Jump if not less or equal
JP Jump if parity PF = 1
JPE Jump if parity even
JNP Jump if not parity PF = 0
JPO Jump if parity odd
JCXZ Jump if CX register is 0 CX = 0
JECXZ Jump if ECX register is 0 ECX = 0
D<Size> – declare initialized data
d<size> initial value Pseudo-instruction <size> filed <size> value
DB byte 1 byte
DW word 2 bytes
DD double word 4 bytes
DQ quadword 8 bytes
DT tenbyte 10 bytes
DDQ double quadword 16 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
Assignment 0
You get a simple program that receives a string from a user. Then, this program calls
to a function (that you should implement in assembly) that receives a string as an
argument and does the following:
1.Convert the uppercase letters to their equivalent Leet symbol, according to the
table below.
2.All other uppercase letters should be converted to lowercase letters
3.Return the number of letters converted to Leets in the input string
Letter Leet symbol
A 4
B 8
Example: C (
>Enter a string: HELLO WorlD! E 3
G 6
>Result string: #3110 world! H #
I !
>Number of letters converted to Leet: 5 L 1
O 0
S 5
The function returns the number of characters which aren’t T 7
Z 2
uppercase or lowercase letter (the output should be just the number) .
The characters conversion should be in-place.
main.c
#include <stdio.h>
# define MAX_LEN 100 // Maximal line size
int main(void) {
char str_buf[MAX_LEN];
int str_len = 0;
Go to http://www.cs.bgu.ac.il/facilities/labs.html
Choose any free Linux computer