1 / 23

Practical Session 1

Computer Architecture and Assembly Language. Practical Session 1. Data Representation Basics. Bit – b asic i nformation uni t : (1/0). Byte – sequence of 8 bits:. 7. 6. 5. 4. 3. 2. 1. 0.

ornelas
Download Presentation

Practical Session 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Architecture and Assembly Language Practical Session 1

  2. Data Representation Basics • Bit–basic information unit: (1/0) • Byte – sequence of 8 bits: 7 6 5 4 3 2 1 0 MSB (Most Significant Bit) LSB (Least Significant Bit) • Main Memory is an array of bytes, addressed by 0 to 232-1=0xFFFFFFFF • 232 bytes = 4∙210∙3 bytes = 4 G bytes address space physical memory • Word– a sequence of bits addressed as a single entity by the computer byte byte 16 bit word

  3. Registers Register file - CPU unit which contains (32 bit) registers. general purpose registersEAX, EBX, ECX, EDX (Accumulator, Base, Counter, Data) index registersESP, EBP, ESI, EDI(Stack pointer - contains the address of last used dword in the stack, Base pointer, Source index, Destination Index) flag register / status registerEFLAGS Instruction Pointer / Program CounterEIP / EPC - contains address (offset) of the next instruction that is going to be executed (at run time)- changed by unconditional jump, conditional jump, procedure call, and return instructions Register file Extended High byte Low byte 16-bit register 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, 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: • MOVAL, 61h ; load AL with 97 decimal (61 hex) • binary code: • 1011000001100001 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)

  5. Basic Assembly Instruction Structure label:(pseudo) instructionoperands; comment either required or forbidden by an instruction optional fields RAM • each instruction has its offset (address) • 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 2048 64 bytes Examples: movax, 2; moves constant 2 to the register axbuffer: resb64 ; reserves 64 bytes  mov buffer, 2 = mov 2048, 2 Notes: - 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  mov [buffer], 2 = mov [2048], 2  mov byte [buffer], 2 = mov byte [2048], 2

  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, 2 mov [buffer], ax target operand source operand target operand source operand register immediate memory location register mov [var1],[var2] ! Note that x86 processor does not allow both operands be memory locations.

  7. MOV - Move Instruction – copies source to destination mov reg8/mem8(16,32),reg8/imm8(16,32) (copies content of register / immediate (source) to register / memory location (destination)) 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 reg16 mem16 imm16 mem16 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.

  8. 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) ADD - add integers Example:add AX, BX;(AX gets a value of AX+BX) SUB - subtract integers 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)(source / destination - register / memory location) INC - increment integer Example: inc AX ;(AX gets a value of AX+1) DEC -increment integer Example: dec byte [buffer] ;([buffer] gets a value of [buffer] -1)

  10. Basic Logical Instructions <instruction> reg8/mem8(16,32)(source / destination - register / memory location) 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 –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)

  11. 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) 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) 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)

  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),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, 11111100bmov bl, 00000010bcmp al, bl ;(ZF (zero flag) gets a value 0) mov al, 11111100bmov bl, 11111100 bcmp 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, 1inc_again: inc eaxjmpinc_again mov ebx, eax this is infinite loop ! this instruction is never reached from this code

  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, 1inc_again: inc eax cmp eax, 10jneinc_again ; if eax ! = 10, go back to loop Example: mov eax, 1inc_again: inc eax cmp eax, 10jeend_of_loop ; if eax = = 10, jump to end_of_loopjmpinc_again ; go back to loop end_of_loop:

  15. Jcc: Conditional Branch

  16. d<size> – declare initialized data d<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 word var: dw ‘AB‘; 0x41 0x42 var: dw ‘ABC' ; 0x41 0x42 0x43 0x00 – complete to word var: dd 0x12345678 ; 0x78 0x56 0x34 0x12

  17. 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: • let n - the fourth digit in the smaller id (of the partners’ ids), if n = 0 then you take the next digit in the id. (1<=n<=9) • Add n to each character of the input string to get the result string. • Count the number of letters in the input string that are converted to non-letter character in the result string. The function returns the result string and the counter. The characters conversion should be in-place. > stuvwxyz > wxyz{|}~ > 4 > abcd > efgh > 0 examples:

  18. main.c #include <stdio.h> # define MAX_LEN 100 // Maximal line size extern int strToLeet (char*); int main(void) { char str_buf[MAX_LEN]; int count= 0; fgets(str_buf, MAX_LEN, stdin); // Read user's command line string count = add_Str_N(str_buf); // Your assembly code function printf("%s\n%d\n",str_buf,count); }

  19. myasm.s 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 add_Str_N; makes the function appear in global scope extern printf ; tell linker that printf is defined elsewhere ; (not used in the program) add_Str_N: ; 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 answer label_here: ; Your code goes somewhere around here... inc ecx ; increment pointer cmp byte [ecx], 0 ; check if byte pointed to is zero jnz 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

  20. Running NASM 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

  21. Example (n=4)

  22. 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

  23. Ascii table

More Related