Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Architecture and System Programming Laboratory

Similar presentations


Presentation on theme: "Computer Architecture and System Programming Laboratory"— Presentation transcript:

1 Computer Architecture and System Programming Laboratory
TA Session 4

2 Local Labels Definition
valid characters in labels are: letters, numbers, _, $, ~, ., and ? first character can be: letter, _, ?, and . Local Labels Definition A label beginning with a single period (.) is treated as a local label, which means that it is associated with the previous non-local label. Example: label1: mov eax, 3 .loop: dec eax jne .loop ret label2: mov eax, 5 Each JNE instruction jumps to the closest .loop, because the two definitions of .loop are kept separate. There is no such notion of scope with local labels. Any such label can be accessed from anywhere, using the full, qualified path through the relevant non-local label (this is indeed label1.loop) (this is indeed label2.loop)

3 Assembly program with no gcc usage
GNU Linker section .data numeric: DD 0x string: DB 'abc' answer: DQ 0 section .text global _start ;entry point (main) _start: mov rdi, 1 ; first argument mov rsi, 2 ; second argument CALL myFunc ; call the function myFunc returnAddress: mov [answer], rax ; retrieve return value from rax add rsp, 8 ; "delete" function arguments mov rax, ; exit program syscall myFunc: push rbp ; save previous value of rbp mov rbp, rsp ; set rbp to point to myFunc frame mov rax,rdi ; get function argument #1 myFunc_code: add rax, rsi ; add function argument #2 returnFrom_myFunc: mov rsp, rbp ; delete frame of myFunc pop rbp ; restore frame of main RET ; return to the caller ld links together compiled assembly without using .c main file > nasm –f elf64 asm.s –o asm.o > ld asm.o –o asm > asm or with gdb debugger > gdb asm Command-line arguments ld (_start) vs. gcc (main (int argc, char** argv)) stack argv[argc-1] argv[2] argv[1] argv[0] argv argc stack argv[argc-1] argv[2] argv[1] argv[0] argc pointer to last command line argument pointer to first command line argument pointer to program name or path RSP+8 RSP RSP (start of main frame) (start of main frame)

4 gdb-GNU Debugger – very basic usage
run Gdb from the console by typing: gdb executableFileName add breaking points by typing: break label start debugging by typing: run parameters (argv) (gdb) set disassembly-flavor intel — change presentation of assembly-language instructions from the default Motorala conventions, that are used by gcc, to the Intel conventions that are used by nasm, that is, from opcode source, dest to opcode dest, src (gdb) layout asm — this will display the assembly language (gdb) layout regs – this will display registers si – one step forward c – continue to run the code until the next break point. q – quit gdb p/x $eax – prints the value in eax x $rsp+8 – prints the address in esp + 8 hexadecimal and the value (dword) that stores in this address. It is possible to use label instead of esp. Type x again will print the next dword in memory.

5 Producing a listing file: > nasm -f elf64 asm.s -l asm.list
first column is the line number in the listing file second column is the relative address of where the code will be placed in memory each section starts at relative address 0 third column is the compiled code forth column is the original code labels do not create code; they are a way to tell assembler that those locations have symbolic names. 0x13 is how many bytes RIP should jump forward ‘CALL myFunc’ is compiled to opcode E8 followed by a 8-byte target address, relative to the next instruction after the call.  address of myFunc label = 0x22  address of the next instruction after the call (i.e. ‘mov [answer], rax’) is 0xF  0x22-0xF=0x13, and we get exactly the binary code written here ‘E ’ executable

6 Addressing Mode specifies how to calculate effective memory address of an operand
x86 64-bit addressing mode rule: up to two of the 64-bit registers and a 64-bit signed constant can be added together to compute a memory address. One of the registers can be optionally pre-multiplied by 2, 4, or 8. Example of right usage mov eax, [rbx] ; move 4 bytes at the address contained in RBX int EAX mov [var], ebx ; move the contents of EBX into 4 bytes at address “var” mov eax, [rsi-4] ; move 4 bytes at address RSI+(-4) into EAX mov [rsi+rax], cl ; move the contents of CL into address RSI+RAX mov edx, [rsi+4*rbx] ; move 4 bytes at address RSI+4*EBX into EDX mov dword [myArray + rbx*4 + rax], ecx ; move the content of 4 bytes at address myArray + RBX*4 + RAX ; to ECX Examples of wrong usage mov eax, [rbx-rcx] ; can only add register values mov [rax+rsi+rdi], ebx ; at most 2 registers in address computation

7 Addressing Modes - Example
section data result: dq 0 section text global dot_product dot_product: enter push rbx push rcx push rdx mov rcx, 0 .dot_product_start: cmp ecx, rdx je .DotProduct_end mov eax, dword [rdi + (4*rcx)] cdq imul dword [rsi + (4*rcx)] add dword [result], eax adc dword [result+4], edx inc rcx jmp .dot_product_start .dot_product_end: mov rax, [result] ; return value pop rdx pop rcx pop rbx leave Since the function is called from C, the function is allowed to mess up the values of EAX, ECX and EDX registers. EBX, ESI, and EDI registers’ values should be preserved and restored by the function. #include <stdio.h> #define VECTOR_SIZE 5 extern long long dot_product (int V1[VECTOR_SIZE], int V2[VECTOR_SIZE], int size); int main () { int V1[VECTOR_SIZE] = {1,0,1,0,2}; int V2[VECTOR_SIZE] = {1,0,1,0,-2}; long result = dot_product(V1,V2,VECTOR_SIZE); printf (“%#lx\n ", result); return 0; { But in fact it is not 100% sure that all C code obeys this (may be compiler implementation dependent), so it is a good idea to save/restore all registers used by the function MUL / IMUL - multiply unsigned / singed numbers Why are two different instructions needed? Example: MUL: 0xFF x 0xFF = 255 x 255 = (0xFE01) IMUL: 0xFF x 0xFF = (−1) x (−1) = 1 (0x0001)


Download ppt "Computer Architecture and System Programming Laboratory"

Similar presentations


Ads by Google