Computer Architecture and System Programming Laboratory

Slides:



Advertisements
Similar presentations
NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
Advertisements

Department of Computer Science and Software Engineering
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
80x86 Instruction Set Dr. Qiang Lin.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
1 Lecture 7 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
8086 : INSTRUCTION SET By, Pramod Sunagar Assistant Professor
Practical Session 3 Computer Architecture and Assembly Language.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 5 Arithmetic and Logic Instructions.
Shift and Rotate Instructions
Practical Session 2. Labels Definition valid characters in labels are: letters, numbers, _, $, ~,., and ? first character can be: letter, _, ? and.
Practical Session 2. Labels Definition valid characters in labels are: letters, numbers, _, $, ~,., and ? first character can be: letter, _, ? and.
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
Assembly Language for Intel-Based Computers
Assembly Language – Lab 5
Ch. 7 Logic, Shift and Rotate instr.
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
Multiplication and Division Instructions & the 0Ah function.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Arithmetic Flags and Instructions
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Integer Arithmetic Computer Organization and Assembly Languages Yung-Yu Chuang 2007/12/24 with slides by Kip Irvine.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Practical Session 2. Flags Register (Status Register) A flag is a single bit of information whose meaning is independent from any other bit Each flag.
Chapter 7: Integer Arithmetic. 2 Chapter Overview Shift and Rotate Instructions Shift and Rotate Applications Multiplication and Division Instructions.
Irvine, Kip R. Assembly Language for Intel-Based Computers. Chapter 7: Integer Arithmetic Slides to Accompany Assembly Language for Intel-Based Computers,
ICS 312 SET 10 Multiplication & Division & input using function 0Ah.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Practical Session 3 Computer Architecture and Assembly Language.
Practical Session 3.
Computer Architecture and Assembly Language
Credits and Disclaimers
Data Transfers, Addressing, and Arithmetic
16.317: Microprocessor System Design I
Computer Architecture and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Practical Session 2.
Microprocessor Systems Design I
Chapter 3 Bit Operations
Microprocessor Systems Design I
Basic Assembly Language
Multiplication and Division Instructions
Assembly Language Programming Part 2
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
CS 301 Fall 2002 Assembly Instructions
X86’s instruction sets.
Chapter 4: Instructions
Practical Session 2.
Practical Session 4.
Shift & Rotate Instructions)
Multiplication and Division Instructions
Assembly Language for Intel-Based Computers, 4th Edition
Shift & Rotate Instructions)
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
X86 Assembly Review.
Chapter 5 Arithmetic and Logic Instructions
Multiplication and Division Instructions
CSC 497/583 Advanced Topics in Computer Security
Credits and Disclaimers
Division instruction.
Shift and Rotate Instructions.
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Computer Architecture and System Programming Laboratory TA Session 3

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

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

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

Loop instruction LOOP, LOOPE, LOOPZ, LOOPNE, LOOPNZ – loop with counter (CX,ECX or RCX) Example: mov ax, 1 mov cx, 3 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.

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, 80008000h mov eax, 20002000h mul ebx ; edx:eax  10002000:10000000h

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

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 ,10110111b ; AL = 10110111b shr AL, 1 ; shift right 1 bit AL = 01011011b, CF = 1 shr AL, CL ; shift right 3 bits AL = 00001011b, 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 ,10110111b sar AL, 1 ; shift right 1 AL = 11011011b sar AL, CL ; shift right 3 AL = 11111011b SAL 10000001b ; the result is 00000010b and this is right result! How? The below explains this. 10000001b = -127 11111111b = -1 (-127) + (-1) = 10000001b + 11111111b = 10000000b = -128 (-128) + (-1) = 10000000b + 11111111b = 01111111b = 127 Since the range of 8 bit signed arithmetic is -128 : 127 it is natural that -128-1 = 127 and that 127 + 1 = -128.  SAL 10000001b = SAL -127 = -127 * 2 = -127 -127 = -128 – 1 – 125 = 127 – 125 = 2 = 00000010b

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 ,10110111b ; BH = 10110111b rol BH, 1 ; rotate left 1 bit  BH = 01101111b rol BH, CL ; rotate left 3 bits  BH = 01111011b RCL, RCR –bitwise rotate on first operand and Carry Flag Example: mov BH ,10110111b ; BH = 10110111b , CF = 0 rcl BH, 1 ; rotate left 1 bit with CF  BH = 01101110b , CF = 1 EQU - define constant Example: foo: EQU 1 ; foo = 1

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 – 35 + 33 = 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

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.

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 ∗ 3 + 4 ∗ 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

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)