Computer Architecture and Assembly Language

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

C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
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.
Practical Session 3 Computer Architecture and Assembly Language.
Assembly Language for Intel-Based Computers Chapter 5: Procedures 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.
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.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
Assembly Language – Lab 5
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
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.
CET 3510 Microcomputer Systems Tech. Lecture 2 Professor: Dr. José M. Reyes Álamo.
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.
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.
Assembly 07. Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data.
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,
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Computer Architecture and Assembly Language
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.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Practical Session 3 Computer Architecture and Assembly Language.
Practical Session 3.
Stack Operations Dr. Hadi AL Saadi.
Computer Architecture and Assembly Language
Practical Session 2.
Computer Architecture and Assembly Language
Chapter 4 Data Movement Instructions
Chapter 3 Bit Operations
Basic Microprocessor Architecture
Assembly Language Programming Part 2
Chapter 4: Instructions
BIC 10503: COMPUTER ARCHITECTURE
Practical Session 2.
Computer Architecture and Assembly Language
Assembly Language Programming II: C Compiler Calling Sequences
Practical Session 4.
Shift & Rotate Instructions)
Multiplication and Division 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
Computer Architecture and System Programming Laboratory
Computer Organization and Assembly Language
Shift and Rotate Instructions.
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
CS-401 Computer Architecture & Assembly Language Programming
Presentation transcript:

Computer Architecture and Assembly Language Practical Session 3

Advanced Instructions – division DIV r/m - unsigned integer division IDIV r/m - signed integer division Dividend Divisor Quotient Remainder AX r/m8 AL AH DX:AX r/m16 DX EDX:EAX r/m32 EAX EDX 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 dx,0 ; clear dividend, high mov ax, 8003h ; dividend, low mov cx, 100h ; divisor DIV cx ; ax = 0080h, dx = 0003h ; quotient = 80h, remainder = 3

Advanced Instructions – shift <instruction> r/m8(16,32) 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

Advanced Instructions - rotate <instruction> r/m8(16,32) 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

Advanced Instructions - loop 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 LOOPE ≡ LOOPZ: jumps if the counter ≠ 0 and Zero Flag = 1 LOOPNE ≡ LOOPNZ: jumps if the counter ≠ 0 and Zero Flag = 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, or code designed to run on a processor operating in 32-bit mode. The syntax is BITS 16 or BITS 32.

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

PUSH - push data on stack Stack Operations PUSH - 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) POP - 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 pop bx ; ax = 0x12AF ; bx = 3 stack stack stack stack stack ESP ESP push ax 0x00 0x03 push bx 0x00 0x03 0x12 0xAF pop ax 0x00 0x03 0x12 0xAF 0x00 0x03 0x12 0xAF pop bx ESP ESP ESP

PUSHFD (push flags double) - push value of EFLAGS onto the stack Stack Operations PUSHAD (push all double) - pushes values of EAX, ECX, EDX, EBX, ESP, EBP, ESI, and EDI onto the stack PUSHFD (push flags double) - push value of EFLAGS onto the stack original ESP value before PUSHAD POPAD (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 flags double) - pop a dword and stores it in EFLAGS stack stack stack stack stack ESP ESP EAX ECX EDX EBX ESP EBP ESI EDI EAX ECX EDX EBX ESP EBP ESI EDI EFLAGS EAX ECX EDX EBX ESP EBP ESI EDI EFLAGS EAX ECX EDX EBX ESP EBP ESI EDI EFLAGS PUSHAD PUSHFD POPFD POPHAD ESP ESP ESP

Calling Convention stack ESP ESP ESP ESP ESP ESP ESP ESP ESP main: ; caller code pushad ; backup registers pushfd ; backup flags register push dword 2 ; push argument #2 push dword 1 ; push argument #1 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, argumentsSize ; "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, localsSize ; 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, return value ; put return value into EAX mov esp, ebp ; move EBP to ESP pop ebp ; restore old EBP RET ; return from the function stack ESP PUSHAD ESP PUSHFD ESP 2 EBP + 12 ESP 1 EBP + 8 ESP return address (of “mov [answer], eax”) ESP EBP value (address of the frame of main) EBP ESP local variables space EBP - 4 ESP … ESP

Run Gdb from the console by typing gdb executableFileName Gdb-GNU Debugger Run Gdb from the console by typing gdb executableFileName 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.

Assignment 1 Task 1 Converting number in hexadecimal base to ASCII characters Examples: Input: 33343536  Output: 3456 Input: 4c656d6f6e Output: Lemon Input: 373E393D46616C7365 Output: 7>9=False

Task 2 practice parameters passing from assembly to C and vice versa Write a C program: read two integer (32 bits) unsigned decimal numbers x, k from user call a function ‘calc_div (int x, int k)’ written in ASSEMBLY Write an assembly function ‘calc_div' that runs the following loop: call a C function 'check (int x, int k)' to check if the numbers are legal either x or k, or both are not legal, print "x or k, or both are off range“, and exit compute z = (int) x div 2^k call printf to print z in decimal Write a C function 'check (int x, int k)': return false if x is negative return false if k is non-positive or greater than 31 return true otherwise Example: > task2.bin 5 1 2  5 div 2^1 = 2

C file for task 1 #include <stdio.h> #define BUFFER_SIZE (128) extern int my_func (char* buf); int main (int argc, char** argv) { char buf [BUFFER_SIZE]; flush (stdout); fgets (buf, BUFFER_SIZE, stdin); printf ("%s", buf); my_func (buf); return 0; }

Assembly file for task 1 ; Your code should be here... section .rodata LC0: DB "%s", 10, 0 ; Format string section .bss LC1: RESB 256 section .text align 16 global my_func extern printf my_func: push ebp mov ebp, esp ; Entry code - set up ebp and esp pushad ; Save registers mov ecx, dword [ebp+8] ; Get argument (pointer to string) ; Your code should be here... push LC1 ; Call printf with 2 arguments: pointer to str push LC0 ; and pointer to format string. call printf add esp, 8 ; Clean up stack after call popad ; Restore registers mov esp, ebp ; Function exit code pop ebp ret