0% found this document useful (0 votes)
7 views16 pages

Lecture Slides 04 041 x86 Mov Swap

The document outlines a roadmap for understanding memory and data management in programming, specifically focusing on x86 assembly language and its interaction with C and Java. It covers key concepts such as data types, memory addressing modes, and basic instruction types, along with examples of code in both C and assembly. Additionally, it provides a detailed explanation of the swap function in assembly, illustrating how data is moved between registers and memory.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Lecture Slides 04 041 x86 Mov Swap

The document outlines a roadmap for understanding memory and data management in programming, specifically focusing on x86 assembly language and its interaction with C and Java. It covers key concepts such as data types, memory addressing modes, and basic instruction types, along with examples of code in both C and assembly. Additionally, it provides a detailed explanation of the swap function in assembly, illustrating how data is moved between registers and memory.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

University

 of  Washington  

Roadmap   Memory  &  data  


Integers  &  floats  
Machine  code  &  C  
C:   Java:  
x86  assembly  
car *c = malloc(sizeof(car)); Car c = new Car(); Procedures  &  stacks  
c->miles = 100; c.setMiles(100); Arrays  &  structs  
c->gals = 17; c.setGals(17);
float mpg = get_mpg(c); float mpg =
Memory  &  caches  
free(c); c.getMPG(); Processes  
Virtual  memory  
Assembly   get_mpg: Memory  allocaIon  
language:   pushq %rbp Java  vs.  C  
movq %rsp, %rbp
...
popq %rbp
ret
OS:  
Machine   0111010000011000
100011010000010000000010
code:   1000100111000010
110000011111101000011111

Computer  
system:  

x86  
University  of  Washington  

SecIon  4:  x86  Assembly  Programming  


¢ Move  instrucIons,  registers,  and  operands  
¢ Memory  addressing  modes
¢ swap  example:  32-­‐bit  vs.  64-­‐bit  
¢ ArithmeIc  operaIons  
¢ CondiIon  codes  
¢ CondiIonal  and  uncondiIonal  branches  
¢ Loops  
¢ Switch  statements  

x86  
University  of  Washington  

Three  Basic  Kinds  of  InstrucIons  


¢ Transfer  data  between  memory  and  register  
§ Load  data  from  memory  into  register  
%reg  =  Mem[address]    
§ Remember:  
§ Store  register  data  into  memory   memory  is  indexed  
just  like  an  array[]!  
§ Mem[address]  =  %reg  

¢ Perform  arithmeIc  funcIon  on  register  or  memory  data  


§ c  =  a  +  b;  

¢ Transfer  control  
§ Uncondi9onal  jumps  to/from  procedures  
§ Condi9onal  branches  

x86  
University  of  Washington  

Moving  Data:  IA32   %eax


%ecx
¢ Moving  Data   %edx
§ movx Source,  Dest
%ebx
§ x  is  one  of  {b, w, l}  
%esi
§ movl  Source,  Dest:   %edi
 Move  4-­‐byte  “long  word”   %esp
§ movw  Source,  Dest:  
%ebp
 Move  2-­‐byte  “word”  
§ movb  Source,  Dest:  
 Move  1-­‐byte  “byte”  

¢ Lots  of  these  in  typical  code  

x86  
University  of  Washington  

Moving  Data:  IA32   %eax


¢ Moving  Data   %ecx
movl  Source,  Dest:   %edx
¢ Operand  Types   %ebx
§ Immediate:  Constant  integer  data   %esi
Example:  $0x400,  $-533  
§ %edi
§ Like  C  constant,  but  prefixed  with  ‘$’
%esp
§ Encoded  with  1,  2,  or  4  bytes  
§ Register:  One  of  8  integer  registers   %ebp
§ Example:  %eax, %edx
§ But  %esp and  %ebp reserved  for  special  use  
§ Others  have  special  uses  for  par9cular  instruc9ons  
§ Memory:  4  consecu9ve  bytes  of  memory  at  address  given  by  register  
§ Simplest  example:  (%eax)
§ Various  other  “address  modes”  
x86  
University  of  Washington  

movl  Operand  CombinaIons  

Source   Dest   Src,Dest   C  Analog  

Reg   movl $0x4,%eax var_a = 0x4;


Imm  
Mem   movl $-147,(%eax) *p_a = -147;

Reg   movl %eax,%edx var_d = var_a;


movl Reg  
Mem   movl %eax,(%edx) *p_d = var_a;

Mem   Reg   movl (%eax),%edx var_d = *p_a;

Cannot  do  memory-­‐memory  transfer  with  a  single  instruc<on.  

x86  
University  of  Washington  

Memory  Addressing  Modes:  Basic  


¢ Indirect  (R)  Mem[Reg[R]]  
§ Register  R  specifies  the  memory  address  
 
movl (%ecx),%eax

¢ Displacement  D(R)  Mem[Reg[R]+D]  


§ Register  R  specifies  a  memory  address  
§ (e.g.  the  start  of  some  memory  region)  
§ Constant  displacement  D  specifies  the  offset  from  that  address  
 
movl 8(%ebp),%edx

x86  
University  of  Washington  

Using  Basic  Addressing  Modes  


swap:
pushl %ebp
movl %esp,%ebp Set  
void swap(int *xp, int *yp) pushl %ebx Up  
{
int t0 = *xp; movl 12(%ebp),%ecx
int t1 = *yp; movl 8(%ebp),%edx
*xp = t1; movl (%ecx),%eax
*yp = t0; Body  
movl (%edx),%ebx
}
movl %eax,(%edx)
movl %ebx,(%ecx)

movl -4(%ebp),%ebx
movl %ebp,%esp
popl %ebp Finish  
ret

x86  
University  of  Washington  

Understanding  Swap  
void swap(int *xp, int *yp) •  
{ •   Stack  
int t0 = *xp; • (in  memory)  
Offset  
int t1 = *yp;
*xp = t1; 12 yp
*yp = t0; 8 xp
}
4 Rtn  adr  
0 Old  %ebp   %ebp
-4 Old  %ebx  
Register  Value  
%ecx yp movl 12(%ebp),%ecx # ecx = yp
%edx xp movl 8(%ebp),%edx # edx = xp
%eax t1 movl (%ecx),%eax # eax = *yp (t1)
%ebx t0 movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
x86  
University  of  Washington  
Address  
Understanding  Swap   123 0x124
456 0x120
0x11c
%eax 0x118
%edx Offset  
0x114
%ecx yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
4 Rtn  adr   0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
x86  
University  of  Washington  
Address  
Understanding  Swap   123 0x124
456 0x120
0x11c
%eax 0x118
%edx Offset  
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
4 Rtn  adr   0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
x86  
University  of  Washington  
Address  
Understanding  Swap   123 0x124
456 0x120
0x11c
%eax 0x118
%edx 0x124 Offset  
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
4 Rtn  adr   0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
x86  
University  of  Washington  
Address  
Understanding  Swap   123 0x124
456 0x120
0x11c
%eax 456 0x118
%edx 0x124 Offset  
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
4 Rtn  adr   0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
x86  
University  of  Washington  
Address  
Understanding  Swap   123 0x124
456 0x120
0x11c
%eax 456 0x118
%edx 0x124 Offset  
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx 123
4 Rtn  adr   0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
x86  
University  of  Washington  
Address  
Understanding  Swap   456 0x124
456 0x120
0x11c
%eax 456
456 0x118
%edx 0x124 Offset  
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx 123
4 Rtn  adr   0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
x86  
University  of  Washington  
Address  
Understanding  Swap   456 0x124
123 0x120
0x11c
%eax 456 0x118
%edx 0x124 Offset  
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx 123
4 Rtn  adr   0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 12(%ebp),%ecx # ecx = yp
%ebp 0x104 movl 8(%ebp),%edx # edx = xp
movl (%ecx),%eax # eax = *yp (t1)
movl (%edx),%ebx # ebx = *xp (t0)
movl %eax,(%edx) # *xp = eax
movl %ebx,(%ecx) # *yp = ebx
x86  

You might also like