Assembly Language: Accessing Memory and Data Types in IA-32 Architecture - Prof. Mirela Da, Study notes of Operating Systems

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

Pre 2010

Uploaded on 02/25/2010

koofers-user-bv1
koofers-user-bv1 🇺🇸

10 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
CSC 2400: Computer Systems
Assembly: Accessing Memory
2
Instructions to Recognize
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Assembly Language: Accessing Memory and Data Types in IA-32 Architecture - Prof. Mirela Da and more Study notes Operating Systems in PDF only on Docsity!

1

CSC 2400: Computer Systems

Assembly: Accessing Memory

2

Instructions to Recognize

3

• Two Operand Instructions

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

Arithmetic Instructions (1)

4

• One Operand Instructions

incl Dest Dest = Dest + 1 decl Dest Dest = Dest - 1 negl Dest Dest = - Dest notl Dest Dest = ~ Dest

Arithmetic Instructions (2)

7

Accessing Memory

Slides by Jennifer Rexford from Princeton University, slightly modified by Mirela Damian.

8

Lecture Goals

• Help you learn…

o To manipulate data of various sizes o To leverage more sophisticated addressing modes

• Focusing on the assembly-language code

o Rather than the layout of memory for storing data

• Why?

o Understand the relationship to data types and common programming constructs in higher-level languages

9

Variable Sizes in High-Level Language

• C data types vary in size

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

• Programmer-created types

o Struct: arbitrary size, depending on the fields

• Arrays

o Multiple consecutive elements of some fixed size o Where each element could be a struct

10

Supporting Different Sizes in IA-

• Three main data sizes

o Byte (b): 1 byte o Word (w): 2 bytes o Long (l): 4 bytes

• Separate assembly-language instructions

o E.g., addb, addw, and addl

• Separate ways to access (parts of) a register

o E.g., %ah or %al, %ax, and %eax

• Larger sizes (e.g., struct)

o Manipulated in smaller byte, word, or long units

13

Byte Order in Multi-Byte Entities

• Intel is a little endian architecture

o Least significant byte of multi-byte entity is stored at lowest memory address o “Little end goes first”

• Some other systems use big endian

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

Little Endian Example

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:

C Example: One-Byte Data

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:

C Example: Four-Byte Data

int i; … if (i > 5) { i++; else i--; }

Global int variable i is in %eax, the full 32 bits of the “A” register.

19

Direct Addressing

• Load or store from a particular memory location

o Memory address is embedded in the instruction o Instruction reads from or writes to that address

• IA-32 example: movl 2000 , %ecx

o Four-byte variable located at address 2000 o Read four bytes starting at address 2000 o Load the value into the ECX register

• Useful when the address is known in advance

o Global variables in the Data or BSS sections

• Can use a label for (human) readability

o E.g., “i” to allow “movl i, %eax”

20

Indirect Addressing

• Load or store from a previously-computed address

o Register with the address is embedded in the instruction o Instruction reads from or writes to that address

• IA-32 example: movl (%eax) , %ecx

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

• Useful when address is not known in advance

o Dynamically allocated data referenced by a pointer o The “(%eax)” essentially dereferences a pointer

21

Base Pointer Addressing

• Load or store with an offset from a base address

o Register storing the base address o Fixed offset also embedded in the instruction o Instruction computes the address and does access

• IA-32 example: movl 8(%eax), %ecx

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

• Useful when accessing part of a larger variable

o Specific field within a “struct” o E.g., if “age” starts at the 8th^ byte of “student” record

22

Indexed Addressing

• Load or store with an offset and multiplier

o Fixed based address embedded in the instruction o Offset computed by multiplying register with constant o Instruction computes the address and does access

• IA-32 example: movl 2000(,%eax,4), %ecx

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)

• Useful to iterate through an array (e.g., a[i])

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

Address Computation Examples

%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

Address Computation Instruction

• leal Src , Dest

o Src is address mode expression o Set Dest to address denoted by expression

• Uses

o Computing address without doing memory reference

  • E.g., translation of p = &x[i]; o Computing arithmetic expressions of the form x + k*y + z
  • k = 1, 2, 4, or 8.
  • z is an 8-bit signed constant

27

Using leal for Arithmetic Expressions

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

Understanding arith

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

Data Transfer Instructions

  • mov{b,w,l} source, dest o General move instruction
  • push{w,l} source pushl %ebx # equivalent instructions subl $4, %esp movl %ebx, (%esp)
  • pop{w,l} dest popl %ebx # equivalent instructions movl (%esp), %ebx addl $4, %esp

esp

esp

esp

esp

32

Conclusions

• Accessing data

o Byte, word, and long-word data types o Wide variety of addressing modes

• Next time

o Calling functions, using the stack