Practical Session 1 Computer Architecture and Assembly Language.
-
Upload
hilary-robyn-wilcox -
Category
Documents
-
view
236 -
download
2
Transcript of Practical Session 1 Computer Architecture and Assembly Language.
Practical Session 1
Computer Architecture and Assembly Language
• Byte – sequence of 8 bits:
16 35 4 07 2
MSB (Most Significant Bit) LSB (Least Significant Bit)
• Bit – basic information unit: (1/0)
• Word – a sequence of bits addressed as a single entity by the computer
byte byte 16 bit word
• Main Memory is an array of bytes, addressed by 0 to 232-1=0xFFFFFFFF
232 bytes = 4∙210∙3 bytes = 4 G bytes
…
…
232-1
2K-1
10
address space
physical memory
Data Representation BasicsData Representation Basics
Register file - CPU unit which contains (32 bit) registers.
general purpose registersEAX, EBX, ECX, EDX EAX, EBX, ECX, EDX (Accumulator, Base, Counter, Data)
index registersESP, EBP, ESI, EDIESP, EBP, ESI, EDI(Stack pointer - contains the address of last used dword in the stack, Base pointer, Source index, Destination Index)
flag register / status registerEFLAGSEFLAGS
Instruction Pointer / Program Counter EIP / EPCEIP / EPC- - contains address (offset) of the next instruction that is going to be executed (at run timeat run time)- - changed by unconditional jump, conditional jump, procedure call, and return instructions
RegistersRegisters
Note that the list of registers above is partial. The full list can be found here.
Register file
High byte
Low byte
Extended
16-bit register
Assembly Language ProgramAssembly 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 codeassembly code:
MOV AL, 61h ; load AL with 97 decimal (61 hex)
binary codebinary code:
10110000 01100001
1011 a binary code (opcode) of instruction 'MOV'
0 specifies if data is byte (‘0’) or full size 16/32 bits (‘1’)
000 a binary identifier for a register 'AL'
01100001 a binary representation of 97 decimal (97d = (int)(97/16)*10 + (97%16 converted to hex digit) = 61h)
label: (pseudo) instruction operands ; comment
optional fieldseither required or forbidden by an instruction
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
Examples:
mov ax, 2 ; moves constant 2 to the register axbuffer: resb 64 ; reserves 64 bytes
Basic Assembly Instruction StructureBasic Assembly Instruction Structure
A typical instruction has 2 operands- target operand (left)- source operand (right)
3 kinds of operands exists- immediate : value- register : AX,EBP,DL etc.- memory location : variable or pointer
Instruction ArgumentsInstruction Arguments
mov [var1],[var2]! ! Note that x86 processor does not Note that x86 processor does not allow both operands be memory locations.allow both operands be memory locations.
Examples:
mov ax, 2 mov [buffer], ax
source operand target operand immediateregister
source operand
target operand registermemory location
mov reg8/mem8mov reg8/mem8(16,32),,reg8/imm8reg8/imm8(16,32)
(copies content of register / immediate (source) to register / memory location (destination))
mov reg8mov reg8(16,32),,reg8/reg8/mem8mem8(16,32)
(copies content of register / memory location (source) to register (destination))
MOV - MOV - Move Instruction – Move Instruction – copies source to destinationcopies source to destination
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.
operands have to be of the same size
Examples:
mov eax, 0x2334AAFF mov [buffer], ax mov wordword [var], 2
imm32reg32 reg16mem16 imm16mem16
ADDADD - add integers
Example:add AX, BX ;(AX gets a value of AX+BX)
ADCADC - add integers with carry (value of Carry Flag)
Example:adc AX, BX ;(AX gets a value of AX+BX+CF)
Basic Arithmetical InstructionBasic Arithmetical Instruction
<instruction> reg8/mem8<instruction> reg8/mem8(16,32),,reg8/imm8reg8/imm8(16,32)
(source - register / immediate, destination- register / memory location)
<instruction> reg8<instruction> reg8(16,32),,reg8/mem8reg8/mem8(16,32)
(source - register / immediate, destination - register / memory location)
SUB - subtract integers
Example:sub AX, BX ;(AX gets a value of AX-BX)
SBB - subtract with borrow (value of Carry Flag)
Example:sbb AX, BX ;(AX gets a value of AX-BX-CF)
Basic Arithmetical InstructionBasic Arithmetical Instruction
<instruction> reg8/mem8<instruction> reg8/mem8(16,32) (source / destination - register / memory location)
INC INC - increment integer
Example: inc AX ;(AX gets a value of AX+1)
DEC DEC - increment integer
Example: dec byte [buffer] ;([buffer] gets a value of [buffer] -1)
Basic Logical InstructionsBasic Logical Instructions
<instruction> reg8/mem8<instruction> reg8/mem8(16,32) (source / destination - register / memory location)
NOT NOT – one’s complement negation – inverts all the bits
Example: mov al, 11111110b not al ;(AL gets a value of 00000001b) ;(11111110b + 00000001b = 11111111b)
NEG 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 = 1100000000b = 0)
Basic Logical InstructionsBasic Logical Instructions
OR OR – bitwise or – bit at index i of the destination gets ‘1’ if bit at index i of source or destination are ‘1’; otherwise ‘0’
Example: mov al, 11111100 b
mov bl, 00000010b
or AL, BL ;(AL gets a value 11111110b)
<instruction> reg8/mem8<instruction> reg8/mem8(16,32),,reg8/imm8reg8/imm8(16,32)
(source - register / immediate, destination- register / memory location)
<instruction> reg8<instruction> reg8(16,32),,reg8/mem8reg8/mem8(16,32)
(source - register / immediate, destination - register / memory location)
ANDAND– 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 reg8/mem8cmp reg8/mem8(16,32),,reg8/imm8reg8/imm8(16,32)
(source - register / immediate, destination- register / memory location)
cmp reg8cmp reg8(16,32),,reg8/mem8reg8/mem8(16,32)
(source - register / immediate, destination - register / memory location)
CMP – CMP – Compare Instruction – Compare Instruction – compares integerscompares integers
Examples:
mov al, 11111100b mov bl, 00000010b
cmp al, bl ;(ZF (zero flag) gets a value 0)
CMP performs a ‘mental’ subtraction - affects the flags as if the subtraction had taken place, but does not store the result of the subtraction.
mov al, 11111100b mov bl, 11111100 b
cmp al, bl ;(ZF (zero flag) gets a value 1)
• 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
LabelLabel – specifies instruction’s offset (address) – specifies instruction’s offset (address)
Examples:
my_instructionmy_instruction: add ax, ax ;(my_instruction is an address of ‘add ax, ax’ instruction)
bufferbuffer: db 0add byte [bufferbuffer], 2 ;([buffer] gets a value of [buffer]+2)
JMP tells the processor that the next instruction to be executed is located at the label that is given as part of jmp instruction.
JMPJMP – unconditional jump – unconditional jump
jmp label jmp label
Example:
mov eax, 1inc_again:
inc eaxjmpjmp inc_againmov ebx, eax
this is infinite loop !
this instruction is never reached from this code!
• 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
J<Condition>J<Condition> – conditional jump – conditional jump
j<cond> label j<cond> label
Example:read_char:
… ; get a character into AL cmp al, ‘a’ ; compare the character to ‘a’
jeje a_received ; if value of al register equals to ‘a’, jump to a_received
jmpjmp read_char ; go back to read another
a_received:…
Instruction Description FlagsJO Jump if overflow OF = 1JNO Jump if not overflow OF = 0JS Jump if sign SF = 1JNS Jump if not sign SF = 0JE 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
Jcc: Conditional BranchJcc: Conditional Branch
D<Size>D<Size> – declare initialized data – declare initialized data
d<size> initial valued<size> initial value
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 word0x00 – complete to word
var: dw 'ab‘ ; 0x41 0x42
var: dw 'abc' ; 0x41 0x42 0x43 0x00 – complete to word0x00 – complete to word
var: dd 0x12345678 ; 0x78 0x56 0x34 0x12
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
Assignment 0Assignment 0
Letter Leet symbolA 4B 8C (E 3G 6H #I !L 1O 0S 5T 7Z 2
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
Example:
>Enter a string: HELLO WorlD!
>Result string: #3110 world!
>Number of letters converted to Leet: 5
The function returns the number of characters which aren’t uppercase or lowercase letter (the output should be just the number) . The characters conversion should be in-place.
#include <stdio.h>
# define MAX_LEN 100 // Maximal line size
extern int strToLeet (char*);
int main(void) {
char str_buf[MAX_LEN];
int str_len = 0;
printf("Enter a string: ");
fgets(str_buf, MAX_LEN, stdin); // Read user's command line string
str_len = strToLeetstrToLeet (str_buf); // Your assembly code function
printf("\nResult string:%s\nNumber of letters converted to Leet: %d\n",str_buf,str_len);
}
main.cmain.c
section .data ; data section, read-write an: DD 0 ; this is a temporary var
section .text ; our code is always in the .text section global strToLeetstrToLeet ; makes the function appear in global scope extern printf ; tell linker that printf is defined elsewhere
; (not used in the program)
strToLeetstrToLeet: ; functions are defined as labels push ebp ; save Base Pointer (bp) original value mov ebp, esp ; use base pointer to access stack contents pushad ; push all variables onto stack mov ecx, dword [ebp+8] ; get function argument
;;;;;;;;;;;;;;;; FUNCTION EFFECTIVE CODE STARTS HERE ;;;;;;;;;;;;;;;;
mov dword [an], 0 ; initialize answerlabel_here:
; Your code goes somewhere around here...; Your code goes somewhere around here...
inc ecx ; increment pointercmp byte [ecx], 0 ; check if byte pointed to is zerojnz label_here ; keep looping until it is null terminated
;;;;;;;;;;;;;;;; FUNCTION EFFECTIVE CODE ENDS HERE ;;;;;;;;;;;;;;;; popad ; restore all previously used registers mov eax,[an] ; return an (returned values are in eax) mov esp, ebp pop ebp ret
myasm.smyasm.s
To assemble a file, you issue a command of the form
>nasm -f <format> <filename> [-o <output>] [ -l listing]
Example:
>nasm -f elf myasm.s -o myelf.o
It would create myelf.o file that has elf format (executable and linkable format).We use main.c file (that is written in C language) to start our program, and sometimes also for input / output from a user. So to compile main.c with our assembly file we should execute the following command:
gcc –m32 main.c myelf.o -o myexe.out
The -m32 option is being used to comply with 32- bit environment
It would create executable file myexe.out.In order to run it you should write its name on the command line:
>myexe.out
Running NASMRunning NASM
How to run Linux from Window Go to http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
Run the following executable
Use “lvs.cs.bgu.ac.il” or “lace.cs.bgu.ac.il” host nameand click ‘Open’
Use your Linux username and password to login lace server
Go to http://www.cs.bgu.ac.il/facilities/labs.html
Choose any free Linux computer
Connect to the chosen computer by using “ssh –X cs302six1-4” (maybe you would be asked for your password again)
cd (change directory) to your working directory
Ascii tableAscii table