Computer Architecture and System Programming Laboratory

Slides:



Advertisements
Similar presentations
COMP 2003: Assembly Language and Digital Logic
Advertisements

Department of Computer Science and Software Engineering
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
Introduction to X86 assembly by Istvan Haller
Practical Session 3 Computer Architecture and Assembly Language.
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.
Position Independent Code self sufficiency of combining program.
Shift and Rotate Instructions
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
CE302 Outline Multiplication Division Program Segment Prefix Command Line Parameters.
Assembly Language – Lab 5
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Multiplication and Division Instructions & the 0Ah function.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Computer Architecture and Assembly Language. Byte structure : a byte has 8 bits MSB (Most Significant Bit) LSB (Least Significant Bit) Data Representation.
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.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.b: Arithmetic Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
Practical Session 4 Computer Architecture and Assembly Language.
Irvine, Kip R. Assembly Language for Intel-Based Computers. Chapter 7: Integer Arithmetic Slides to Accompany Assembly Language for Intel-Based Computers,
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
ICS 312 SET 10 Multiplication & Division & input using function 0Ah.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
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.
Multiplication and Division instructions Dr.Hadi AL Saadi.
Practical Session 3 Computer Architecture and Assembly Language.
Precept 7: Introduction to IA-32 Assembly Language Programming
Practical Session 3.
Computer Architecture and Assembly Language
Practical Session 5.
Instruction Set Architecture
Computer Architecture and Assembly Language
Credits and Disclaimers
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Exploiting & Defense Day 2 Recap
Introduction to Compilers Tim Teitelbaum
Basic Assembly Language
Multiplication and Division Instructions
X86’s instruction sets.
Computer Architecture and Assembly Language
Chapter 4: Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Microprocessor and Assembly Language
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Assembly Language Programming II: C Compiler Calling Sequences
Practical Session 4.
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 and System Programming Laboratory
EECE.3170 Microprocessor Systems Design I
X86 Assembly Review.
Computer Architecture and System Programming Laboratory
Multiplication and Division Instructions
Multiplication and Division Instructions
Credits and Disclaimers
Division instruction.
Computer Architecture and System Programming Laboratory
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Computer Architecture and System Programming Laboratory TA Session 4 MUL, DIV Dot Product code example (self-study) ld Assembler (_start, exit) gdb debugger

Advanced Instructions - multiplication MUL r/m - unsigned integer multiplication IMUL r/m - signed integer multiplication Multiplicand Multiplier Product AL r/m8 AX r/m16 DX:AX EAX r/m32 EDX:EAX MUL / IMUL - multiply unsigned / singed numbers Why are two different instructions needed? Example: MUL: 0xFF x 0xFF = 255 x 255 = 65025 (0xFE01) IMUL: 0xFF x 0xFF = (−1) x (−1) = 1 (0x0001) MUL r/m8 mov al, 9 ; multiplicand mov bl, 5 ; multiplier mul bl ; = 0x2D AX 0x00 0x2D MUL r/m16 mov ax, 0x2000 mov bx, 0x8000 mul bx ; = 0x10000000 DX AX 0x10 0x00 0x00 MUL r/m32 mov eax, 0x20002000 mov ebx, 0x80008000 mul ebx ; = 0x1000200010000000 EDX EAX 0x10 0x00 0x20 0x10 0x00

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, 0x83 ; dividend mov bl, 0x02 ; divisor DIV bl ; al = 0x41 quotient, ah = 0x01 remainder AX BL AL AH / = 0x00 0x83 0x02 0x41 and 0x01 reminder

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 IDIV r/m16 mov ax, 0xFFFF ; dividend, low-part register mov cx, 0x100 ; divisor IDIV cx ; ax = 0x0080 quotient, dx = 0x0003 remainder required: 0xFFFF/ 0x100 = -1/0x100 DX AX DX AX 0x10 0x02 0xFF 0x00 0xFF   executed: 0x01002FFFF / 0x100 executed: 0x00000FFFF / 0x100 = 65,535 / 100

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 for r/m32 use ‘cdq’ to convert EAX doubleword to EDX:EAX quadword IDIV r/m16 mov ax, 0xFFFF ; dividend, low-part register mov cx, 0x100 ; divisor cwd ; convert AX word to DX:AX double word by copying MSB of AX to all the bits of DX IDIV cx ; ax = 0x0080 quotient, dx = 0x0003 remainder required: 0xFFFF/ 0x100 = -1/0x100 DX AX 0x11 0xFF  executed: 0xFFFFFFFF / 0x100

Multiplication – Code Example self reading stack SIZE b a return address EBP previous value #include <stdio.h> #define SIZE 5 extern long long * DotProduct (int a[SIZE], int b[SIZE], int size); void main () { int a[SIZE] = {1,0,1,0,2}; int b[SIZE] = {1,0,1,0,-2}; long long * result = DotProduct(a, b, SIZE); printf (“%#llx\n ", result); { EBP + 16 EBP + 12 EBP + 8 EBP ESP

stack self reading next element of vector a next element of vector b section .data result: dd 0,0 section .text global DotProduct DotProduct: push ebp mov ebp, esp push ebx pushad mov ecx, 0 DotProduct_start: mov edx, 0 cmp ecx, dword [ebp+16] je DotProduct_end mov ebx, dword [ebp+8] mov eax, dword [ebx + (4*ecx)] mov ebx, dword [ebp+12] imul dword [ebx + (4*ecx)] add dword [result], eax adc dword [result+4], edx inc ecx jmp DotProduct_start DotProduct_end: mov eax, result ; return value pop edx pop ecx pop ebx mov esp, ebp pop ebp ret self reading stack VECTOR_SIZE b a return address EBP previous value old ebx old ecx old edx EBP + 16 EBP + 12 EBP + 8 EBP ESP next element of vector a next element of vector b

Assembly program with no C file usage GNU Linker section .data … section .bss section .rodata section .text global _start ; entry point _start: mov ebx,0 ; exit system call mov eax,1 int 0x80 ld links assembly object file(s) > nasm –f elf32 asm.s –o asm.o > ld -m elf_i386 asm.o –o asm > ./asm Command-line arguments ld (_start) vs. gcc (main (int argc, char** argv)) stack pointer to last command line argument argv[argc-1] … argv[2] argv[1] argv[0] argv argc stack argv[argc-1] … argv[2] argv[1] argv[0] argc use ‘main’ entry point label in your pure assembly code to compile it with gcc pointer to first command line argument pointer to program name ESP+4 ESP ESP (start of main frame) (start of main frame)

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 (command line arguments) (gdb) set disassembly-flavor intel — change presentation of assembly-language instructions from the default Motorola conventions, that are used by gdb, to the Intel conventions that are used by nasm, that is, from ‘opcode source, dest’ to ‘opcode dest, src’ (gdb) layout asm – display assembly language (gdb) layout regs – 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– prints esp value (address) and value (dword) that is stored in this address. It is possible to use label instead of esp. Type x again will print the next dword in memory.

print numeric global variable section .data numeric: DD 0x12345678 string: DB 'abc' answer: DD 0 section .text global _start _start: pushad push dword 2 push dword 1 CALL myFunc returnAddress: mov [answer], eax add esp, 8 popad mov ebx,0 mov eax,1 int 0x80 myFunc: push ebp mov ebp, esp mov eax, dword [ebp+8] mov ebx, dword [ebp+12] myFunc_code: add eax, ebx returnFrom_myFunc: mov esp, ebp pop ebp ret print numeric global variable numeric into memory – little endian print string global variable string into memory – little endian pushad 0xffffd640 – 0xffffd620= 0x20 = 32 bytes = 8 registers * 4 bytes push function’s arguments into stack CALL myFunc return address