Download presentation
Presentation is loading. Please wait.
Published byBudi Setiawan Modified over 6 years ago
1
Computer Architecture and System Programming Laboratory
TA Session 1
2
Data Representation Basics
bit – basic information unit: (1/0) 3 2 1 nibble – sequence of 4 bits: 7 6 5 4 3 2 1 byte – sequence of 8 bits: word – sequence of 2 (or 4) bytes 16 bit word 32 bit word byte byte main memory 264-1 2K-1 1 … address space: 0 to 264-1= 0xFFFFFFFFFFFFFFFF 264 bytes = 24∙260 bytes = 24∙230 Gb = 16 exabytes address space physical memory
3
general purpose registers
Registers file - CPU unit which contains registers general purpose registers r8 – r15 r8d r8w r8b Extended High Low flag register RFLAGS Instruction Pointer register RIP - contains address of the next instruction that is going to be executed (at run time) - changed by unconditional jump, conditional jump, procedure call, and return instructions Note that the list of registers above is partial. The full list can be found here.
4
Assembly Language Program
consists of a series of processor instructions, assembler directives, comments, and data translated by assembler into machine language instructions (binary code) that can be loaded into memory and executed NASM - Netwide Assembler - is an assembler and for x86 architecture Example: assembly code: MOV AL, 0x ; load AL with 97 decimal (61 hex) machine code (in binary): a binary opcode of instruction 'MOV' specifies if data is byte (‘0’) or full size 16/32 bits (‘1’) a binary identifier for a register 'AL' a binary representation of 97 decimal (97d = (int)(97/16)*10 + (97%16 converted to hex digit) = 61h)
5
label: (pseudo) opcode operands ; comment
Basic structure of an assembly instruction label: (pseudo) opcode operands ; comment optional fields either required or forbidden by instruction each instruction has its address in memory we mark an instruction with a label to refer it in the code (non-local) labels have to be unique an instruction that follows a label can be at the same / next line colon is optional RAM … Examples: mov ax, ; moves constant 2 to the register ax buffer: resb ; reserves 64 bytes 64 bytes buffer 2048 mov buffer, 2 mov 2048, 2 mov [buffer], 2 mov [2048], 2 mov byte [buffer], 2 mov byte [2048], 2 move to one byte starting from buffer address in memory numeric value 2 - backslash (\) : 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
6
Instruction Arguments
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 Examples: mov ax, mov [buffer], ax target operand source operand target operand source operand register immediate memory location register ! Note that we cannot have both source and target operands specified as memory locations. mov [var1],[var2]
7
MOV - Move Instruction – copies source to target
mov reg8/mem8(16,32,64),reg8/imm8(16,32,64) (copies content of register / immediate (source) to register / memory location (target)) mov reg8(16,32,64),reg8/mem8(16,32,64) (copies content of register / memory location (source) to register (target)) 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 size 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.
8
Basic Arithmetical Instruction
<instruction> reg8/mem8(16,32,64),reg8/imm8(16,32,64) (source - register / immediate, - register / memory location) <instruction> reg8(16,32,64),reg8/mem8(16,32,64) (source - register / immediate, - register / memory location) carry means the digit with promote to higher powers, whereas borrow means the digit we demote to lower powers ADD - add integers without carry Example: add AX, BX ;(AX gets a value of AX+BX) SUB - subtract integers without carry Example: sub AX, BX ;(AX gets a value of AX-BX) ADC - add integers with carry (value of Carry Flag) Example: adc AX, BX ;(AX gets a value of AX+BX+CF) SBB - subtract with borrow (value of Carry Flag) Example: sbb AX, BX ;(AX gets a value of AX-BX-CF)
9
Basic Arithmetical Instruction
<instruction> reg8/mem8(16,32,64) (source / - register / memory location) INC - increment integer Example: inc AX ;(AX gets a value of AX+1) DEC - decrement integer Example: dec byte [buffer] ;(first byte of [buffer] gets a value of first byte of [buffer] -1)
10
Basic Logical Instructions
<instruction> reg8/mem8(16,32,64) (source / - register / memory location) NOT – one’s complement negation – inverts all the bits Example: mov al, b not al ;(AL gets a value of b) ;( b b = b) NEG – two’s complement negation – inverts all the bits, and adds 1 Example: mov al, b neg al ;(AL gets a value of not( b)+1= b+1= b) ;( b b = b = 0)
11
Basic Logical Instructions
<instruction> reg8/mem8(16,32,64),reg8/imm8(16,32,64) (source - register / immediate, - register / memory location) <instruction> reg8(16,32,64),reg8/mem8(16,32,64) (source - register / immediate, - register / memory location) OR – bitwise or – bit at index i of the gets ‘1’ if bit at index i of source or are ‘1’; otherwise ‘0’ Example: mov al, b mov bl, b or AL, BL ;(AL gets a value b) AND– bitwise and – bit at index i of the gets ‘1’ if bits at index i of both source and are ‘1’; otherwise ‘0’ Example: or AL, BL ;(with same values of AL and BL as in previous example, AL gets a value 0)
12
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,64),reg8/imm8(16,32,64) (source - register / immediate, - register / memory location) cmp reg8(16,32,64),reg8/mem8(16,32,64) (source - register / immediate, - register / memory location) Examples: mov al, b mov bl, b cmp al, bl ;(ZF (zero flag) gets a value 0) mov al, b mov bl, b cmp al, bl ;(ZF (zero flag) gets a value 1)
13
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: mov eax, 1 inc_again: inc eax jmp inc_again mov ebx, eax this is infinite loop ! this instruction is never reached from this code rip register gets inc_again label (address)
14
J<Condition> – conditional jump
j<cond> 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 inc_again: inc eax cmp eax, 10 jne inc_again ; if eax ! = 10, go back to loop Example: mov eax, 1 inc_again: 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:
15
Note that the list above is partial. The full list can be found here.
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 Note that the list above is partial. The full list can be found here.
16
d<size> – declare initialized data
d<size> initial value <size> value <size> filed Pseudo-instruction 1 byte byte DB 2 bytes word DW 4 bytes double word DD 8 bytes quadword DQ 10 bytes tenbyte DT 16 bytes double quadword DDQ octoword DO Examples: var: db x55 ; define a variable var of size byte, initialized by 0x55 var: db x55,0x56,0x57 ; three bytes in succession var: db 'a‘ ; character constant 0x61 (ascii code of ‘a’) var: db 'hello’,10 ; string constant var: dw x1234 ; 0x34 0x12 var: dw ‘A' ; 0x41 0x00 – complete to word var: dw ‘ABC' ; 0x41 0x42 0x43 0x00 – complete to word var: dd x ; 0x78 0x56 0x34 0x12
17
Assignment 0 You get a simple program that receives a string from the user. Than, it calls to a function (that you’ll implement in assembly) that receives one string as an argument and should do the following: Convert lower case to upper case. Convert ‘(’ into ‘<’. Convert ‘)’ into ‘>’. Count the number of the non-letter characters, that is ANY character that is not 'a'->'z' or 'A'->'Z‘, including ‘\n’ character The function shall return the number of the letter characters in the string. The characters conversion should be in-place. Example: 42: heLLo() WorLd! 42: HELLO<> WORLD! 9
18
main.c #include <stdio.h>
# define MAX_LEN /* Maximal line size */ extern int do_Str (char*); int main(void) { char str_buf[MAX_LEN]; int counter = 0; fgets(str_buf, MAX_LEN, stdin); /* Read user's command line string */ counter = do_Str (str_buf); /* Your assembly code function */ printf("%s%d\n",str_buf,counter); return 0; }
19
myasm.s section .data ; data section, read-write
an: DQ ; this is a temporary var section .text ; our code is always in the .text section global do_Str ; makes the function appear in global scope extern printf ; tell linker that printf is defined elsewhere ; (not used in the program) do_Str: ; functions are defined as labels push rbp ; save Base Pointer (bp) original value mov rbp, rsp ; set base pointer to point on function frame mov rcx, rdi ; get function argument ;;;;;;;;;;;;;;;; FUNCTION EFFECTIVE CODE STARTS HERE ;;;;;;;;;;;;;;;; mov aword [an], 0 ; initialize answer label_here: ; Your code goes somewhere around here... inc rcx ; increment pointer cmp byte [rcx], 0 ; check if byte pointed to is zero jnz label_here ; keep looping until it is null terminated ;;;;;;;;;;;;;;;; FUNCTION EFFECTIVE CODE ENDS HERE ;;;;;;;;;;;;;;;; mov rax,[an] ; return an (returned values are in rax) mov rsp, rbp pop rbp ret
20
8 bytes of quadword variable an
suppose that register RCX contains address of (first character of) input string str_buf suppose an is a name of counter that counts non letters characters of str_buf an is defined in the second line of the assembly code below an is of type DQ, this means Define Quadword, i.e. 64-bit sized variable using RCX, get to first character of str_buf, and change it according to the assignment requirements if this character is not letter, increment the value of an Example: str_buf and an at the beginning of do_Str function execution: RAM … 10 ‘!’ ‘e’ ‘h’ 8 bytes of quadword variable an an ‘\n’ ASSCII code input string str_buf RCX
21
Running NASM To assemble a file, you issue a command of the form
> nasm -f <format> <filename> [-o <output>] [ -l listing] Example: > nasm -f elf64 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 main.c myelf.o -o myexe.out If we do not use main.c, we compile with gcc as follows: gcc myelf.o -o myexe.out It would create executable file myexe.out. In order to run it you should write its name on the command line: > myexe.out
22
How to run Linux from Window
Go to Run the following executable Use “lvs.cs.bgu.ac.il” or “lace.cs.bgu.ac.il” host name and click ‘Open’ Use your Linux username and password to login lace server Go to Computer-Labs-building-34-and-95.aspx Choose any free Linux computer Connect to the chosen computer by using “ssh –X cs302row2-2” (maybe you would be asked for your password again) cd (change directory) to your working directory
23
ASCII table
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.