Basic Memory Address

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Basic Memory Address as PDF for free.

More details

  • Words: 882
  • Pages: 13
CS220 Feb 21, 2007

Basic Memory Address .section .data value: .int 1 movl value, %eax #moving the content at the address into register movl $3, %ebx movl %ebx, value movl $value, %ecx #Moving the address into register

Indirect Memory Access movl %ebx, %ecx overwrite ecx by the content in ebx movl %ebx, (%ecx) write the content in ebx to the memory address, which is stored in ecx movl %ebx, 4(%ecx) write the content in ebx to the memory address+4; the address is stored in ecx movl %ebx, -4(%ecx) write the content in ebx to the memory address-4; the address is stored in ecx Reg Add

Mem

Indexed Memory Mode displacement(base, index, scale) Address=base+index*scale+displacement base and index must be registers, but scale can be numbers, displacement can be label or number movl 8(%eax,%ebx,4), %ecx eax stores address, ebx stores index Can be other registers! Index starts from 0!

Example values: .int 10, 15, 20, 25, 30, 35, 40, 45 … movl values, %eax movl $values, %ebx movl $100, 4(%ebx) movl $1, %ecx movl (%ebx, %ecx,4), %edx OR movl values(, %ecx, 4), %edx

Why are they equivalent?

Memory Addressing •

Direct Operand: displacement – (often just the symbolic name for a memory location)

• •

Indirect Operand: (base) Base+displacement: displacement(base) – index into an array – access a field of a record



(index*scale)+displacement: displacement(,index,scale) – index into an array



Base + index + displacement: displacement(base,index) – two dimensional array – one dimensional array of records



Base+(index*scale)+ displacement: displacement(base, index,scale) – two dimensional array

Example movl movl movl movl movl

8(%ebx),%eax (%ebx,%ecx,4),%eax (%ebx,%ecx),%eax -0x20(%ebx,%ecx,0x4),%eax lable1(,%ecx,0x4),%eax 0 can be omitted, no $

.section .data output: .asciz "the value is %d\n" values: .int 10, 15, 20, 25, 30, 35, 40, 45 .section .text .globl main main: pushl %ebp movl %esp, %ebp

.section .data output: .asciz "the value is %d\n" values: .int 10, 15, 20, 25, 30, 35, 40, 45 .section .text .globl main main: pushl %ebp movl %esp, %ebp movl $0, %edi

movl $0, %edi movl $values, %esi loop:

loop: movl (%esi, %edi, 4), %eax

movl values(, %edi, 4), %eax

pushl %eax pushl $output call printf addl $8, %esp

pushl %eax pushl $output call printf addl $8, %esp

inc %edi cmpl $8, %edi jne loop

inc %edi cmpl $8, %edi jne loop

leave ret

leave ret

Little Endian One Hexdec bit takes FOUR binary bits

Write 0x1234abcd to memory address 0x0000 bytes big-endian little-endian 0x0000 0x12 0xcd 0x0001 0x34 0xab 0x0002 0xab 0x34 0x0003 0xcd 0x12

Big-Endian: MAC, SPARC Little-Endian: Intel, AMD

Stack • Data Section memory low->high • Stack memory high->low • ESP always points to the last element used on the stack (not the first free element) • push[l,w] r/m/I • pop[l,w] r/m

Push/Pop All the registers Push All General-Purpose Registers – PUSHAW pushes, in succession, AX, CX, DX, BX, SP, BP, SI and DI on the stack, decrementing the stack pointer by a total of 16. – PUSHAL pushes, in succession, EAX, ECX, EDX, EBX, ESP, EBP, ESI and EDI on the stack, decrementing the stack pointer by a total of 32. – In both cases, the value of SP or ESP pushed is its original value, as it had before the instruction was executed. – PUSHA is an alias mnemonic for either PUSHAW or PUSHAL, depending on the current BITS setting. Pop All General Purpose Registers – POPAW pops a word from the stack into each of, successively, DI, SI, BP, nothing (it discards a word from the stack which was a placeholder for SP), BX, DX, CX and AX. It is intended to reverse the operation of PUSHAW, but it ignores the value for SP that was pushed on the stack by PUSHAW. – POPAL pops twice as much data, and places the results in EDI, ESI, EBP, nothing (placeholder for ESP), EBX, EDX, ECX and EAX. It reverses the operation of PUSHAL. – POPA is an alias mnemonic for either POPAW or POPAL, depending on the current BITS setting. Pop Flags Register – POPFW pops a word from the stack and stores it in the bottom 16 bits of the flags register (or the whole flags register, on processors below a 386). – POPFL pops a doubleword and stores it in the entire flags register. POPF is an alias mnemonic for either POPFW or POPFL, depending on the current BITS setting.

Example • • • • •

pushl %ecx # puts the 32-bit value of the ECX register on the stack pushw %cx # puts the 16-bit value of the CX register on the stack pushl $100 # puts the value of 100 on the stack as a 32-bit integer value pushl data # puts the 32-bit data value referenced by the data label pushl $data # puts the 32-bit memory address referenced by the data label

• • •

popl %ecx # place the next 32-bits in the stack in the ECX register popw %cx # place the next 16-bits in the stack in the CX register popl value # place the next 32-bits in the stack in the value memory location

Example pushf pushl %edi pushl %esi pushl %edx pushl %ecx pushl %ebx pushl %eax #do something movl $1, %eax movl $2, %edx popl popl popl popl popl popl popf

%eax %ebx %ecx %edx %esi %edi

Related Documents

Address
October 2019 50
Address
October 2019 56
Address
November 2019 50
Address
November 2019 39