Re: Base Address's, etc...(reply from Dan to David)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Dan,

> 	Ah ok, thx for clearing that up for me, at least addition of
> addresses worked the way I thought. What I am also confussed about is
> HOW this works.
>
> For instance, 0x240 expands to 0000 0010 0100 0000 in binary
> (note... I have put the spaces in for readability purposes).
>
>     Now... how does the computer store a "value" in this address space?

I'm still a little confused about what you're asking, but I thought I'd
have a go anyway (it keeps me away from doing work at least  :-)

You've been refering to memory, and to writeb(), so I'm not sure if you're
talking about IO space or memory space.  Since IO space has entered the
conversation I'm going to assume the use of an x86 system.

What you're asking is basically what the machine instructions are to
perform write into various addresses.  The quickest way to see the
computer store a value in an address is to write a short C program
(silly.c) and look at the equivalent assembler:

int main(void) {
  int x;
  x = 5;
  return x;
}

Now the optimiser will want to change this to not use memory, so we need
to compile without optimisation:

# gcc -O0 silly.c -S -o silly.s

Now look at silly.s (I'll just include the main function):

main:
	pushl %ebp
	movl %esp,%ebp
	subl $24,%esp
	movl $5,-4(%ebp)
        xorl %eax,%eax
	jmp .L2
	.p2align 4,,7
.L2:
	leave
	ret

I'll describe this, even though not all of it is what you asked for

Every function uses some stack space (and the stack grows downwards in
memory), so the first three instuctions (pushl/movl/subl) grab some space
for the stack.  In fact it grabs 6 longs (longs and ints are 4 bytes, and
it reserves 24 bytes by moving down the stack base pointer %esp).

Next comes the part you are interested in: movl $5,-4(%ebp)

This moves the number 5 into the memory location pointed to by the stack
base pointer minus 4.  The brackets around %ebp mean to use the address
that %ebp refers to.  Since 4 is the size of an int then it's talking
about the first int under the base of the stack.  OK, so it's not an
absolute number like 0x240, but it's very rare to do something like that.

The last bits just return 0, so you don't have to worry about it.

You could also write to an absolute memory location like 0x240 using movl,
but the address has to come from a register, so you put 0x240 (decimal
576) into a register first:

  movl $576,%eax
  movl $5,(%eax)

To use an offset of 3, then you'd use:

  movl $576,%eax
  movl $5,3(%eax)

But 3 bytes will overlap the long, you probably meant to offset by 3 longs
(which is 12 bytes):

  movl $576,%eax
  movl $5,12(%eax)

However, these are all guaranteed to segfault, because 0x240 is in the
first page of memory space.  Addresses for memory mapped devices are much
higher, like 0xe0000000.  Do a "cat /proc/iomem" to see what is currently
being mapped to what.

>     Also.. as I said earlier, 0x240 expands to 0000 0010 0100 0000.
> So... if you say something like writeb(value, io + 3), then you are
> writing a value to 0000 0010 0100 0011 instead of the orginal 0x240, which
> is what you want to do... but highly confusses me on how it works.

Since you are refering to writeb() you are now talking about port space.
This is similar to memory space, but it uses a different bus, and
different instructions to talk to it.  The addresses are only 16 bits, so
they are a lot smaller.

The resulting assembler is to generate "out" instructions.  outb writes a
byte, outw writes a 16 bit value, outl writes a 32 bit value:

  movw $5,%ax
  outw %ax,$240

for an offset, use a 16 bit register as the io address (here I'll put the
number 5 as a a byte [00000101] into 0x240 and 0x243):

  movb $5,%al      # set 8 bit register "al" to 5
  movw $576,%dx    # set 16 bit register "dx" to 0x240
  outb %al,%dx     # write al to address dx
  addw %dx,$3      # add 3 to dx
  outb %al,%dx     # write al to address dx


Sorry if I've left you confused with all of this.  I hope it explains some
things.

Regards,
Paul Gearon

Software Engineer                Telephone:   +61 7 3876 2188
Plugged In Software              Fax:         +61 7 3876 4899
http://www.PIsoftware.com        PGP Key available via finger

Catapultam habeo. Nisi pecuniam omnem mihi dabis, ad caput tuum saxum
immane mittam.
(Translation from latin: "I have a catapult. Give me all the money,
or I will fling an enormous rock at your head.")


--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux