









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of assembly language instructions for accessing memory and working with different data types in the ia-32 architecture. Topics include arithmetic and logical instructions, variable sizes, addressing modes, and loading and storing data. Understanding these concepts is essential for low-level programming and system development.
Typology: Study notes
1 / 16
This page cannot be seen from the preview
Don't miss anything!










1
2
3
addl Src , Dest Dest = Dest + Src subl Src , Dest Dest = Dest - Src imull Src , Dest Dest = Dest * Src sall Src , Dest Dest = Dest << Src sarl Src , Dest Dest = Dest >> Src Arithmetic shrl Src , Dest Dest = Dest >> Src Logical xorl Src , Dest Dest = Dest ^ Src andl Src , Dest Dest = Dest & Src orl Src , Dest Dest = Dest | Src
4
incl Dest Dest = Dest + 1 decl Dest Dest = Dest - 1 negl Dest Dest = - Dest notl Dest Dest = ~ Dest
7
Slides by Jennifer Rexford from Princeton University, slightly modified by Mirela Damian.
8
o To manipulate data of various sizes o To leverage more sophisticated addressing modes
o Rather than the layout of memory for storing data
o Understand the relationship to data types and common programming constructs in higher-level languages
9
o Character: 1 byte o Short, int, and long: varies, depending on the computer o Float and double: varies, depending on the computer o Pointers: typically 4 bytes
o Struct: arbitrary size, depending on the fields
o Multiple consecutive elements of some fixed size o Where each element could be a struct
10
o Byte (b): 1 byte o Word (w): 2 bytes o Long (l): 4 bytes
o E.g., addb, addw, and addl
o E.g., %ah or %al, %ax, and %eax
o Manipulated in smaller byte, word, or long units
13
o Least significant byte of multi-byte entity is stored at lowest memory address o “Little end goes first”
o Most significant byte of multi-byte entity is stored at lowest memory address o “Big end goes first”
00000101 00000000 00000000 00000000
1000 1001 1002 1003
The int 5 at address 1000:
00000000 00000000 00000000 00000101
1000 1001 1002 1003
The int 5 at address 1000:
14
Byte 0: ff Byte 1: 77 Byte 2: 33 Byte 3: 0
int main(void) { int i=0x003377ff, j; unsigned char *p = (unsigned char *) &i; for (j=0; j<4; j++) printf("Byte %d: %x\n", j, p[j]); }
Output on a little-endian machine
15
cmpb $5, %al jle else incb %al jmp endif else: decb %al endif:
char i; … if (i > 5) { i++; else i--; }
Global char variable i is in %al , the lower byte of the “A” register.
16
cmpl $5, %eax jle else incl %eax jmp endif else: decl %eax endif:
int i; … if (i > 5) { i++; else i--; }
Global int variable i is in %eax, the full 32 bits of the “A” register.
19
o Memory address is embedded in the instruction o Instruction reads from or writes to that address
o Four-byte variable located at address 2000 o Read four bytes starting at address 2000 o Load the value into the ECX register
o Global variables in the Data or BSS sections
o E.g., “i” to allow “movl i, %eax”
20
o Register with the address is embedded in the instruction o Instruction reads from or writes to that address
o EAX register stores a 32-bit address (e.g., 2000) o Read long-word variable stored at that address o Load the value into the ECX register
o Dynamically allocated data referenced by a pointer o The “(%eax)” essentially dereferences a pointer
21
o Register storing the base address o Fixed offset also embedded in the instruction o Instruction computes the address and does access
o EAX register stores a 32-bit base address (e.g., 2000) o Offset of 8 is added to compute address (e.g., 2008) o Read long-word variable stored at that address o Load the value into the ECX register
o Specific field within a “struct” o E.g., if “age” starts at the 8th^ byte of “student” record
22
o Fixed based address embedded in the instruction o Offset computed by multiplying register with constant o Instruction computes the address and does access
o Index register EAX (say, with value of 10) o Multiplied by a multiplier of 1, 2, 4, or 8 (say, 4) o Added to a fixed base of 2000 (say, to get 2040)
o Base is the start of the array (i.e., “a”) o Register is the index (i.e., “i”) o Multiplier is the size of the element (e.g., 4 for “int”)
25
%edx %ecx
0xf 0x
2*0xf000 + 0x
0xf000 + 4*0x
0xf000 + 0x
0xf000 + 0x
Computation
0x80(,%edx,2) 0x1e
(%edx,%ecx,4) 0xf
(%edx,%ecx) 0xf
0x8(%edx) 0xf
Expression Address
26
o Src is address mode expression o Set Dest to address denoted by expression
o Computing address without doing memory reference
27
int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; }
arith: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax movl 12(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 16(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax movl %ebp,%esp popl %ebp ret
Body
Set Up
Finish
28
int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; }
x is at address ebp+ y is at address ebp+ x is at address ebp+ To be explained in next lecture
movl 8(%ebp),%eax # eax = movl 12(%ebp),%edx # edx = leal (%edx,%eax),%ecx # ecx = leal (%edx,%edx,2),%edx # edx = sall $4,%edx # edx = addl 16(%ebp),%ecx # ecx = leal 4(%edx,%eax),%eax # eax = imull %ecx,%eax # eax =
31
esp
esp
esp
esp
32
o Byte, word, and long-word data types o Wide variety of addressing modes
o Calling functions, using the stack