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 3

2 Stack - LIFO word-size data structure
RSP STACK is temporary storage memory area RSP register points on top of stack (by default, it is highest RAM address) stack addresses go from high to low

3 PUSH - push data on stack
Stack Operations PUSH - push data on stack decrements RSP by 2/4/8 bytes (according to the operand size) stores the operand value at RSP address on stack (in Little Endian manner) POP - load a value from the stack reads the operand value at RSP address on stack (in Little Endian manner) increment RSP by 2/4/8 bytes (according to operand size) Example: mov ax, 3 push ax PUSHFQ - push value of RFLAGS into stack POPFQ - pop dword and stores it in RFLAGS pop ax ; ax = 3 stack stack stack RSP RSP push ax 0x00 0x03 0x00 0x03 pop ax RSP

4 int a,b,c,d,e,f,g; long long h,res; void main () { res = myFunc(a,b,c,d,e,f,g,h); } long long myFunc (int a,int b,int c,int d,int e,int f,int g,long long h) { long long xx = 0, yy = 0; int zz = 0; return xx;} main: ; caller code mov rax, 0 ; unless we use floating-point point registers, we should initialize RAX to zero mov rdi, [a] ; push argument #1 mov rsi, [b] ; push argument #2 mov rdx, [c] ; push argument #3 mov rcx, [d] ; push argument #4 mov r8, [e] ; push argument #5 mov r9, [f] ; push argument #6 push qword [h] ; push argument #8 push dword [g] ; push argument #7 call myFunc ; call the function  push return address add rsp, <argumentsSize> ; "delete" function arguments (argumentsSize = qword+dword= 12 bytes) mov [res], rax RBP start of frame of main function stack RSP myFunc: ; callee code push rbp ; backup RBP mov rbp, rsp ; reset RBP to current RSP sub rsp, <localsSize> ; allocate space for local variables (localsSize = 20 bytes) mov qword [rbp-8], 0 ; initialize a local variable xx mov qword [rbp-16], 0 ; initialize a local variable yy mov dword [rbp-20], 0 ; initialize a local variable zz mov eax, [rbp+16] ; get first argument mov rbx, [rbp+20] ; get second argument ; function code mov rax, <returnValue> ; put return value into RAX mov rsp, rbp ; move RBP to RSP pop rbp ; restore old RBP RET ; return from the function RSP RSP RBP RSP RSP

5 Loop instruction LOOP, LOOPE, LOOPZ, LOOPNE, LOOPNZ – loop with counter (CX,ECX or RCX) Example: mov ax, mov cx, my_ loop: add ax, ax loop my_ loop, cx 1. decrements counter register 2. if counter does not become zero as a result of this operation, jump to the given label LOOPE ≡ LOOPZ: jumps if the counter ≠ 0 and ZF = 1 LOOPNE ≡ LOOPNZ: jumps if the counter ≠ 0 and ZF = 0 Note: LOOP instruction does not set any flags Note: if a counter is not specified explicitly, the BITS setting dictates which is used. The BITS directive specifies whether NASM should generate code designed to run on a processor operating in 16-bit mode, 32-bit mode, or 64-bit mode. The syntax is BITS 16,  BITS 32, or BITS 64.

6 Multiplication instruction
MUL r/m - unsigned integer multiplication IMUL r/m - signed integer multiplication Multiplicand Multiplier Result AL r/m8 AX r/m16 DX:AX EAX r/m32 EDX:EAX RAX r/m64 RDX:RAX MUL r/m8 mov bl,5 ; multiplier mov al,9 ; multiplicand mul bl ; ax  2Dh MUL r/m16 mov bx, 8000h mov ax, 2000h mul bx ; dx:ax  1000:0000h MUL r/m32 mov ebx, h mov eax, h mul ebx ; edx:eax  : h

7 Division instruction DIV r/m - unsigned integer division
IDIV r/m - signed integer division Dividend Divisor Quotient Reminder AX r/m8 AL AH DX:AX r/m16 DX EDX:EAX r/m32 EAX EDX RDX:RAX r/m64 RAX RDX For 32 bit division, we should use ‘cdq’ (convert double to quad) For 64 bit division, we should use ‘cqo’ (convert quad to oct) DIV r/m8 mov ax,0083h ; dividend mov bl, 2h ; divisor DIV bl ; al = 41h, ah = 01h ; quotient is 41h, remainder is 1 DIV r/m16 mov ax, 8003h ; dividend, low cwd ; convert word to double, copies the sign bit of AX into every bit of DX mov cx, 100h ; divisor DIV cx ; ax = 0080h, dx = 0003h ; quotient = 80h, remainder = 3

8 Shift instruction <instruction> r/m8(16,32,64) 1/CL/imm8
SHL, SHR – Bitwise Logical Shifts on first operand number of bits to shift is given by second operand vacated bits are filled with zero (last) shifted bit enters the Carry Flag Example: mov CL , 3 mov AL , b ; AL = b shr AL, 1 ; shift right 1 bit AL = b, CF = 1 shr AL, CL ; shift right 3 bits AL = b, CF = 0 Note: shift indeed performs division / multiplication by 2 SAL, SAR – Bitwise Arithmetic Shift on first operand vacated bits are filled with zero for SAL vacated bits are filled with copies of the original high bit of the source operand for SAR Example: mov CL , 3 mov AL , b sar AL, 1 ; shift right 1 AL = b sar AL, CL ; shift right 3 AL = b SAL b ; the result is b and this is right result! How? The below explains this. b = -127 b = -1 (-127) + (-1) = b b = b = -128 (-128) + (-1) = b b = b = 127 Since the range of 8 bit signed arithmetic is -128 : 127 it is natural that = 127 and that = -128.  SAL b = SAL -127 = -127 * 2 = = -128 – 1 – 125 = 127 – 125 = 2 = b

9 Rotate instruction & constants
<instruction> r/m8(16,32,64) 1/CL/imm8 ROL, ROR – bitwise rotate (i.e. moves round) on the first operand Example: mov CL, 3 mov BH , b ; BH = b rol BH, 1 ; rotate left 1 bit  BH = b rol BH, CL ; rotate left 3 bits  BH = b RCL, RCR –bitwise rotate on first operand and Carry Flag Example: mov BH , b ; BH = b , CF = 0 rcl BH, 1 ; rotate left 1 bit with CF  BH = b , CF = 1 EQU - define constant Example: foo: EQU ; foo = 1

10 TIMES - repeat (instruction or data)
TIMES prefix causes the instruction to be assembled multiple times zeroBuf: times 64 db 0 ; 64 bytes initialized to 0 TIMES can be applied to ordinary instructions, so you can code trivial loops mov EAX, 0 times 100 inc EAX ; EAX =100 => loop the argument to TIMES is not just a numeric constant, but a numeric expression buffer: db ‘go’ times 64-$+buffer db ‘!’ ; (64 - $ + buffer = 64 – = 64 – 2 = 62) buffer + 64 96 36 35 34 33 20 ! o g $ (current position in the current section) buffer $$ (start of the current section) - start of section .data

11 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 s/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 $esp+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.

12 Assignment 1 - postfix-based calculator
typedef struct bignum { long number_of_digits; char *digit; } bignum; addition subtraction multiplication division arbitrary-precision integers implement your own stack S (say, 1024 bytes sized array) Example: To compute 2 ∗ ∗ 5, print the result, and quit, we input 2 3 * 4 5 * + p q read input string char by char, ignoring any type of space characters 2 will be pushed onto S 3 will be pushed onto S * will pop the top two elements on S, and push their product, 6, back onto S 4 will be pushed onto S 5 will be pushed onto S * will pop the top two elements on S, and push their product, 20, back onto S + will pop the top two elements on S, and push their sum, 26, onto S p will print the result in base 10 (decimal) q will quit the program Assume only correct input

13 Assignment 1 - postfix-based calculator
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)


Download ppt "Computer Architecture and System Programming Laboratory"

Similar presentations


Ads by Google