Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’...
-
Upload
miles-gibson -
Category
Documents
-
view
217 -
download
1
Transcript of Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’...
![Page 1: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/1.jpg)
Assembly
תכנות באסמבלי
![Page 2: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/2.jpg)
Assembly vs. Higher level languages There are NO variables’ type definitions.
All kinds of data are stored in the same registers. We need to know what we are working it in order to use
the right instructions. Memory = a large, byte-addressable array.
Only a limited set of registers is used to store data while running the program. If we need more room we must save the data into
memory and later reread it.
No special structures (instructions) for “if” / “switch” / “loops” (for, while, do-while), or even functions!
![Page 3: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/3.jpg)
3
תזכורת: תהליך קומפילציה
![Page 4: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/4.jpg)
4
בכתיבת מרובת קבצים Cתמיכת שפת
כתיבת מודולים הוטמעה בכך שבניית Cבשפת
התוכנה נעשית בשני שלבים:
של כל )compilation( הידורהידור1.1. ) בנפרדc.קובץ מקור (
של כל )linking( קישורקישור2.2.הקבצים ביחד
4
file1.c file2.c filen.c
file1.o file2.o filen.o
Compiler Compiler Compiler
Linker
runfile
![Page 5: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/5.jpg)
Linux Compilation Process
Five stages : Preprocessing, Parsing, Translation, Assembling, and Linking
All 5 stages are implemented by one program in UNIX, gcc
gcc is the C compiler of choice for most UNIX. Actually just a front end that executes various other programs corresponding to each stage in the compilation process. To get it to print out the commands it executes at each step, use gcc -v.
5
![Page 6: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/6.jpg)
How to - Disassembly of code Compilation of code:
gcc -c code.c We get the file: code.o
Disassembly: objdump -d code.o We get an assembly-like code that represents the c
code appeared in file code.c Or:
gcc -s code.c We get a code.s file that contains an assembly code
created by the compiler.
![Page 7: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/7.jpg)
Standard data typesAssembly
In Assembly: size = type of variable.
![Page 8: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/8.jpg)
Words, double words …. Due to its origins as a 16-bit architecture that
expanded into a 32-bit one, Intel uses the term “word” to refer to a 16-bit data type and not a 32-bit data type as we mentioned before. Backward Compatibility.
32-bit quantities as “double words”. 64-bit quantities as “quad words”. Most instructions we will encounter operate on bytes
or double words. Each instruction has 3 variants, depending on its
suffix (‘b’ – byte / ‘w’ – word / ‘l’ – double word).
![Page 9: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/9.jpg)
The Registers An IA32 CPU contains a set of eight registers storing
32-bit values. These registers are used to store integer data as well as pointers.
The registers names all begin with %e (extend), but otherwise they have peculiar names.
In the original 8086 CPU each register had a specific target (and hence it got its name). Today most of these targets are less significant. Some instructions use fixed registers as sources and/or
destinations. Within procedures there are different conventions for
saving and restoring the first three registers (%eax, %ecx, and %edx), than for the next three (%ebx, %edi, and %esi).
%ebp and %esp contain pointers to important places in the program stack.
![Page 10: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/10.jpg)
The File Register
![Page 11: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/11.jpg)
Partial access to a register
The low-order two bytes of the first four registers can be independently read or written by the byte operation instructions. This feature was provided to allow backward compatibility.
When a byte instruction updates one of these single-byte “register elements,” the remaining three bytes of the register do not change.
Same goes for the low-order 16 bits of each register, using word operation instructions.
![Page 12: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/12.jpg)
Operand Forms
![Page 13: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/13.jpg)
Move to / from memory Instructions
![Page 14: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/14.jpg)
Important Suffixes
‘l’ - double word. ‘w’ - word. ‘b’ - byte ‘s’ - single (for floating point)
![Page 15: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/15.jpg)
movl Operand Combinations
Cannot do memory-memory transfers with single instruction !!!
movl
Imm
Reg
Mem
Reg
Mem
Reg
Mem
Reg
Source Destination
movl $0x4,%eax
movl $-147,(%eax)
movl %eax,%edx
movl %eax,(%edx)
movl (%eax),%edx
C Analog
temp = 0x4;
*p = -147;
temp2 = temp1;
*p = temp;
temp = *p;
![Page 16: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/16.jpg)
Important prefixes
i – immediate (constant) value. r – register. m – memory.
rrmovl = move double word from one register to the other.
irmovw = move the word given as immediate value into the register.
![Page 17: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/17.jpg)
movb & movw The movb instruction is similar, but it moves just
a single byte. When one of the operands is a register, it must be one of the eight single-byte register elements.
Similarly, the movw instruction moves two bytes. When one of its operands is a register, it must be one of the eight two-byte register elements.
Both the movsbl and the movzbl instructions serve to copy a byte and to set the remaining bits in the destination: movsbl - signed extension. movzbl - zero extension.
![Page 18: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/18.jpg)
MOVSBL and MOVZBL
MOVSBL sign-extends a single byte, and copies it into a double-word destination
MOVZBL expands a single byte to 32 bits with 24 leading zeros, and copies it into a double-word destination
Example: %eax = 0x12345678 %edx = 0xAAAABBBB MOVB %dh, %al %eax = 0x123456BB MOVSBL %dh, %eax %eax = 0xFFFFFFBB MOVZBL %dh, %eax %eax = 0x000000BB
18
![Page 19: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/19.jpg)
Another example
(Assume initially that %dh = 8D, %eax = 98765432)
movb %dh,%al %eax = 9876548D movsbl %dh,%eax %eax = FFFFFF8D movzbl %dh,%eax %eax = 0000008D
![Page 20: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/20.jpg)
C vs. Assembly example
![Page 21: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/21.jpg)
Arithmetic & Logical Operations
![Page 22: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/22.jpg)
Arithmetic & Logical Operations (2)
With the exception of leal, each of these instructions has a counterpart that operates on words (16 bits) and on bytes (by replacing the suffix).
Again, cannot do memory-memory transfers with single instruction
![Page 23: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/23.jpg)
“Load Effective Address” (leal)
The “Load Effective Address” (leal) instruction is actually a variant of the movl instruction.
Its first operand appears to be a memory reference, but instead of reading from the designated location, the instruction copies the effective address to the destination. Doesn’t access memory !!!
This instruction can be used to generate pointers for later memory references without accessing memory.
![Page 24: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/24.jpg)
The leal Instruction can be used to compactly describe common arithmetic operations.
If register %edx contains value x, then the instruction:leal 7(%edx,%edx,4), %eax
will set register %eax to 5x + 7. It is commonly used to perform simple arithmetic:
(%eax = x; %ecx = y) leal 6(%eax), %edx leal (%eax,%ecx), %edx leal (%eax,%ecx,4), %edx leal 7(%eax,%eax,8), %edx leal 0xA(,%ecx,4), %edx leal 9(%eax,%ecx,2), %edx
leal (2)
= x+6
= x+y
= x+4y
= 9x+7
= 4y+10
=x+2y+9
![Page 25: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/25.jpg)
Logical Shift Logical Shift - every bit in the operand is
simply moved a given number of bit positions, and the vacant bit-positions are filled in, usually with zeros.
Useful as multiplication or division of unsigned integers by powers of two.
25
![Page 26: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/26.jpg)
Arithmetic Right Shift A bitwise operation that shifts all of
the bits of its operand Every bit in the operand is simply
moved a given number of bit positions, and the vacant bit-positions are filled in with the leftmost bit.
When shifting to the right - leftmost bit- sign bit - sort of sign extension. Dividing by 2n
26
![Page 27: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/27.jpg)
Either logical or arithmetic
k is a number between 0 and 31, or the single-byte register %cl
Suppose that x and n are stored at memory locations with offsets 8 and 12, respectively, relative to the address in register %ebp
get n get x x <<= 2 x >>= n
Shift
movl 12(%ebp), %ecx
movl 8(%ebp), %eax
sall $2,%eax
sarl %cl,%eax
![Page 28: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/28.jpg)
C vs. Assembly example
![Page 29: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/29.jpg)
mul & div Instructions
![Page 30: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/30.jpg)
Imull Example
mov +4823424, eax
mov -423, ebx
imul ebx ;
What happens is :
EDX:EAX = FFFFFFFFh:86635D80h
תמר שרוט, נועם חזון 30
![Page 31: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/31.jpg)
Cltd - converts the signed long in EAX to a signed double long in EDX:EAX by extending the most-significant bit (sign bit) of EAX into all bits of EDX
iDiv - The idiv instruction divides the contents of the 64 bit integer EDX:EAX (EDX as the most significant four bytes and EAX as the least significant four bytes) by the specified operand value. The quotient result of the division is stored into EAX, while the remainder is placed in EDX.
תמר שרוט, נועם חזון 31
![Page 32: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/32.jpg)
Code example
(x at %ebp+8, y at %ebp+12) movl 8(%ebp),%eax Put x in %eax imull 12(%ebp) Multiply by y pushl %edx Push high-order 32 bits pushl %eax Push low-order 32 bits
![Page 33: Assembly תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions. All kinds of data are stored in the same registers.](https://reader036.fdocuments.in/reader036/viewer/2022062517/56649eb75503460f94bc10aa/html5/thumbnails/33.jpg)
Yet, another example
(x at %ebp+8, y at %ebp+12) movl 8(%ebp),%eax Put x in %eax cltd Sign extend into
%edx idivl 12(%ebp) Divide by y pushl %eax Push x / y pushl %edx Push x % y