user1462787
user1462787

Reputation: 669

Memory access in assembly

How many memory accesses are required in the worst case for the following instructions:

add edx, (to_printf-next_i) ; where to_printf and next_i are labels defined in .text
inc dword [myarray + ebx*4] ; where myarray is a label defined in .data 

Is my answer true?

  1. 0 , since we do not access memory here
  2. fetch: 4 bytes for the address : myarray + ebx*4 -> 2 memory accesses in the worst case
     write: 4 bytes because of "dword" -> 2 memory accesses in the worst case
     read? 

Upvotes: 1

Views: 4241

Answers (1)

Alexey Frunze
Alexey Frunze

Reputation: 62106

inc does 3 things: reads the operand, adds 1 to it, writes the operand back. If the operand is in memory, you normally have 1 read access and 1 write access.

If the operand crosses a cache line boundary, then every access turns into 2 accesses.

But this can be much worse when page translation and virtual memory are enabled and the memory this instruction references is on the disk. TLB misses, other cache misses and all the page table and I/O activity that the OS needs to do to let the instruction execute may incur many more memory accesses. It's nearly impossible to tell how many are needed for this kind of worst case.

Upvotes: 3

Related Questions