Practical Session 3 Computer Architecture and Assembly Language.

Slides:



Advertisements
Similar presentations
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 5 MIXING C AND ASSEMBLY.
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
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.
Computer Organization And Assembly Language
Practical Session 3 Computer Architecture and Assembly Language.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Runtime Stack Managed by the CPU, using two registers
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Practical Session 2. Labels Definition valid characters in labels are: letters, numbers, _, $, ~,., and ? first character can be: letter, _, ? and.
CS2422 Assembly Language & System Programming October 24, 2006.
Practical Session 2. Labels Definition valid characters in labels are: letters, numbers, _, $, ~,., and ? first character can be: letter, _, ? and.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
CDP ECE Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Computer Architecture and Assembly Language Practical session outline Introduction 80x86 assembly –Data storage –The registers –Flags –Instructions Assignment.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Arithmetic Flags and Instructions
UHD:CS2401: A. Berrached1 The Intel x86 Hardware Organization.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Computer Architecture and Assembly Language
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Practical Session 3.
Stack Operations Dr. Hadi AL Saadi.
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Practical Session 2.
Computer Architecture and Assembly Language
Chapter 4 Data Movement Instructions
Introduction to Compilers Tim Teitelbaum
Chapter 3 Bit Operations
Basic Microprocessor Architecture
Machine control instruction
Assembly Language Programming Part 2
Chapter 4: Instructions
BIC 10503: COMPUTER ARCHITECTURE
Practical Session 2.
Computer Architecture and Assembly Language
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Practical Session 4.
Shift & Rotate Instructions)
Multi-modules programming
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Computer Architecture CST 250
Computer Architecture and System Programming Laboratory
X86 Assembly Review.
Chapter 5 Arithmetic and Logic Instructions
CSC 497/583 Advanced Topics in Computer Security
Computer Organization and Assembly Language
Shift and Rotate Instructions.
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Practical Session 3 Computer Architecture and Assembly Language

DIV r/m - unsigned integer division IDIV r/m - signed integer division DividendDivisorQuotientRemainder AXr/m8ALAH DX:AXr/m16AXDX EDX:EAXr/m32EAXEDX Advanced Instructions – division DIV r/m8 movax,0083 h ; dividend movbl, 2 h ; divisor DIVbl ; al = 41 h, ah = 01 h ; quotient is 41 h, remainder is 1 DIV r/m16 movdx,0 ; clear dividend, high movax, 8003 h ; dividend, low movcx, 100 h ; divisor DIVcx ; ax = 0080 h, dx = 0003 h ; quotient = 80 h, remainder = 3

Bitwise Arithmetic Shift SAL, SAR – Bitwise Arithmetic Shift on first operand –vacated bits are filled with zero for SAL original high bit SAR –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 Advanced Instructions – shift Bitwise Logical Shifts 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 r/m8 (16,32) 1/CL/imm8 r/m8 (16,32) 1/CL/imm8 Note: shift indeed performs division / multiplication by 2

ROL, ROR – bitwise rotate (i.e. moves round) on the first operand Example: mov CL, 3 mov BH, b ; BH = b rol BH, 01 ; rotate left 1 bit  BH = b rol BH, CL; rotate left 3 bits  BH = b Advanced Instructions - rotate RCL, RCR – bitwise rotate on first operand and Carry Flag Example: mov BH, b ; BH = b, CF = 0 rcl BH, 01 ; rotate left 1 bit with CF  BH = b, CF = 1 r/m8 (16,32) 1/CL/imm8 r/m8 (16,32) 1/CL/imm8

LOOP, LOOPE, LOOPZ, LOOPNE, LOOPNZ – loop with counter (CX or ECX ) Example: mov ax, 1 mov cx, 3 my_ loop: add ax, ax loop my_ loop, cx 1. decrements its counter register (in this case it is CX register) 2. if the counter does not become zero as a result of this operation, it jumps to the given label 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, or code designed to run on a processor operating in 32-bit mode. The syntax is BITS 16 or BITS 32. LOOPE ≡ LOOPZ: jumps if the counter ≠ 0 and Zero Flag = 1 LOOPNE ≡ LOOPNZ: jumps if the counter ≠ 0 and Zero Flag = 0 Advanced Instructions - loop Note: LOOP instruction does not set any flags

Stack temporary storage memory areaSTACK is temporary storage memory area every cell 2 / 4 bytesevery cell in stack is of size 2 / 4 bytes ESPtop of stackESP register points to the top of stack stack addresses high to lowstack addresses go from high to low Read-only Data Segment.bss.data.text.rodata ESP

Stack Operations PUSH - push data on stackPUSH - push data on stack –decrements ESP by 2 / 4 bytes (according to the operand size) –stores the operand value at ESP address on stack ( in Little Endian manner ) ESP 0x00 0x03 stack ESP 0x00 0x03 0x12 0xAF stack ESP stack push bx pop ax ESP stack pop bx POP load a value from the stackPOP - load a value from the stack –reads the operand value at ESP address on stack ( in Little Endian manner ) –increment ESP by 2 / 4 bytes (according to operand size) Example : mov ax, 3 mov bx, 0x12AF push ax push bx pop ax ; ax = 0x12AF pop bx ; bx = 3 ESP 0x00 0x03 stack push ax

PUSHAD pushes EAX, ECX, EDX, EBX, ESP, EBP, ESI, and EDI onto the stackPUSHAD (push all double) - pushes EAX, ECX, EDX, EBX, ESP, EBP, ESI, and EDI onto the stack PUSHFD push flags register onto the stackPUSHFD (push flags double) - push flags register onto the stack ESP stack ESP stack PUSHAD EAX ECX EDX EBX ESP EBP ESI EDI ESP stack PUSHFD EAX ECX EDX EBX ESP EBP ESI EDI EFLAGS original ESP value before PUSHAD POPAD pop a dword from the stack into each one of EDI, ESI, EBP, nothing, EBX, EDX, ECX, and EAXPOPAD (pop all double) - pop a dword from the stack into each one of (successively) EDI, ESI, EBP, nothing (placeholder for ESP), EBX, EDX, ECX, and EAX POPFD pop a dword and stores it in EFLAGSPOPFD (pop flags double) - pop a dword and stores it in EFLAGS POPFD ESP stack EAX ECX EDX EBX ESP EBP ESI EDI ESP stack POPHAD Stack Operations

Function Calls - Stack Frame main: ; caller code pushad; backup registers pushfd; backup flags register push dword 2; push argument #2 push dword 1; push argument #1 caller CALL myFunc; call the function  the address of the next instruction of caller ; (i.e. return address) is pushed automatically onto the stack mov [answer], eax; retrieve return value from EAX add esp, ; "delete" function arguments (in our case argumentsSize = 2 * dword = 8 bytes) popfd; restore flags register popad; restore registers myFunc:; callee code push ebp; backup EBP mov ebp, esp; reset EBP to the current ESP sub esp, ; allocate space for locals mov ebx, [ebp+12] ; get second argument mov ecx, [ebp+8] ; get first argument mov [ebp-4], ebx ; initialize a local... ; function code mov eax, ; put return value into EAX mov esp, ebp; move EBP to ESP pop ebp ; restore old EBP RET; return from the function stack local variables space PUSHAD PUSHFD 2 1 return address (of “mov [answer], eax”) EBP … EBP + 12 EBP ESP EBP + 8 EBP - 4

Gdb-GNU Debugger gdb  Run Gdb from the console by typing gdb executableFileName points  Adding breaking points by typing: break label  Start debugging by typing: run parameters (argv) si – one step forward c – continue to run the code until the next break point. q – quit gdb p $eax – prints the value in eax x $esp+4 – prints the address in esp + 4 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.

Task 1 Converting "Number in base 4" to "Hexadecimal". You can get up to 32 binary digits. Examples: Input:  Output: Input:  Output: A41 Assignment 1

Task 2 Practice parameters passing from assembly to C and vice versa. Write a C program that: read long long (64 bits) unsigned number x (in hexadecimal) from user read one (32 bits) integer numOfRounds (in decimal) from user call ‘calc_func (long long *x, int numOfRounds)’ written in ASSEMBLY Write an assembly function ‘calc_func' that runs the following loop: calculates a "descriptive number" y call 'compare (long long * x, long long * y)' C function to compare x and y if x == y, print x and break the from the loop if x != y –if the loop was executed less than numOfRounds times set x <– y and continue –else print y and return Example Example: >1AF2345FF23B0001 // x - input >1 // 1 round > // y - output

Task 2 A single round execution explanation: x = 1AF2345FF23B0001 digit - counter of appearances of the digit in x: A - 1 B - 1 C - 0 D - 0 E - 0 F - 3 We get y =

main1.c file for task 1 #include #defineBUFFER_SIZE(128) extern int my_func(char* buf); int main(int argc, char** argv) { char buf[BUFFER_SIZE]; fflush(stdout); fgets(buf, BUFFER_SIZE, stdin); my_func(buf); return 0; }

section.rodata LC0: DB"%s", 10, 0; Format string section.bss LC1: RESB32 section.text align 16 global my_func extern printf my_func: pushebp movebp, esp; Entry code - set up ebp and esp pusha; Save registers mov ecx, dword [ebp+8]; Get argument (pointer to string) ; Your code should be here... pushLC1; Call printf with 2 arguments: pointer to str pushLC0; and pointer to format string. callprintf add esp, 8; Clean up stack after call popa; Restore registers movesp, ebp; Function exit code popebp ret The skeleton of assembly programs in both tasks