-1

In addition to using "mov" to place an immediate value (like $2) into a register, or loading the contents of a register into another register, is there a way to "mov" or load a value into a hex memory address?

I have found the corresponding memory address for pin 8 and 9 on Arduino UNO from the data sheet. I am trying to perform a simple program from my command prompt (X86, Mac using linux docker image) that will put the value "00000011" into both addresses 0x24 and 0x25, thus doing both tasks of setting pin 8 and 9 as output, then output high to the same two pins with LEDs. All I want is to light them up.

I was under the impression that you could address a memory location like 0x24/0x25 easily by "loading" instead of "mov", or maybe even just by saying "mov" $0b00000011 to 0x24/0x25

When I do the "mov" version, my command prompt error is "segmentation fault", which means that the memory location is inaccessible or im addressing it wrong. I would try the "load" version if I knew the right name for that operation in x86.

hb91
  • 1
  • 2
  • Yeah, you *can* execute an instruction that stores to an arbitrary memory location, but under a normal OS most addresses aren't mapped. So the result will be a #PF (page fault) exception, which the OS exception-handler decides is invalid, so it delivers a SIGSEGV to your process. It's not a read vs. write problem unless you happen to hard-code an address that's mapped but read-only, like in the `.text` section. Like a `mov 0x401000, %eax` load. (With default x86-64 linker settings for a non-PIE executable, that's the start of the .text section.) – Peter Cordes May 24 '22 at 15:25
  • 1
    You can use hex representation for your intermediate values, if that's what you're asking. Regardless of hex or base10, the assembled instructions represent the same number in a binary format. – h0r53 May 24 '22 at 15:32
  • Peter Cordes - ok. I have seen some one do this on YouTube, but it was a different version of assembly. it was just two simple lines of code - one to set the arduino pins as outputs, then to write digital high to them...so if the addresses aren't mapped, is it useless to try this experiment? – hb91 May 24 '22 at 15:32
  • actually it was four. two to load the registers with $0b00000011, and then two to put that value from the register into the memory address at 0x24, 0x25 – hb91 May 24 '22 at 15:35
  • 2
    Note that your mac is not an Arduino UNO and does not have GPIO registers mapped at addresses 0x24 or 0x25. If you want to blink LEDs on your Arduino, you'll have to program the Arduino, not your mac. – fuz May 24 '22 at 15:36
  • h0r53 - that's not what im trying to do. my value that I want in address 0x24 and 0x25 is: $0b00000011 – hb91 May 24 '22 at 15:37
  • FUZ - I have seen this same thing done on YouTube. I would normally use Arduino IDE but this is an experiment to see if I can control the Arduino UNO in command prompt using assembly language – hb91 May 24 '22 at 15:38
  • Also related: [Why page faults are usually handled by the OS, not hardware?](https://stackoverflow.com/q/60825210) explains how an OS's page-fault handler works. – Peter Cordes May 24 '22 at 15:42
  • 2
    Anyway, what are you actually doing? Linux in a Docker image isn't an Arduino. Unless you're running AVR instructions inside an Arduino emulator, storing to those absolute addresses isn't going to do anything useful. It's always going to segfault in a user-space process on Linux, because that memory isn't mapped. Assembly language is extremely non-portable; you can't expect microcontroller MMIO registers you saw in a youtube video to exist in the address space of an x86 Linux process. – Peter Cordes May 24 '22 at 15:45
  • 1
    TL:DR: yes, it's useless to try this experiment. There aren't GPIO pins at those addresses anyway on a real x86 PC or Mac, even if you were writing a kernel driver so you could access those physical addresses. – Peter Cordes May 24 '22 at 15:46
  • ok so maybe I am not explaining this right. I have provided a link to a video of someone doing this...so if you're confused, just watch this YouTube video https://www.youtube.com/watch?v=RxWwbaDy_uM&list=WL&index=77 – hb91 May 24 '22 at 15:48
  • is this possible on terminal, command prompt, assembly language?? – hb91 May 24 '22 at 15:49
  • 1
    On an x86, no. I can tell immediately from the title that it's about programming an Arduino UNO, which is a specific hardware board that has GPIO registers at those addresses. (And is a microcontroller that doesn't use virtual memory, so addresses in your program are physical addresses.) Assembly language isn't like Python or even C, the same program doesn't work the same on different systems. There is no portability layer, you have to write code appropriate for the environment you want to run it in. – Peter Cordes May 24 '22 at 16:00
  • If this isn't clear enough, start reading the free book [Programming from the Ground Up](https://savannah.nongnu.org/projects/pgubook/) which explains CPUs and OSes as well as asm. See errata https://savannah.nongnu.org/bugs/?group=pgubook and/or HTML chapters: https://programminggroundup.blogspot.com/2007/01/chapter-1-introduction.html – Peter Cordes May 24 '22 at 16:02
  • Peter Cordes - I thought maybe there was a way for the arduino to hear what you're putting into a certain memory address or register on the Mac. apparently not...? I would think there would be SOME way to do this – hb91 May 24 '22 at 16:08
  • Peter Cordes - did you watch the video? – hb91 May 24 '22 at 16:09
  • 1
    I opened the page and read the title before the video loaded. That was sufficient to know it was about programming for an arduino, which I understand the basics of already. I'm not interested in watching a video about programming for an Arduino; if you had a link to asm text I'd be able to skim it, but there's not really any point. BTW, if you want anyone to get notified of your replies, tag them with `@username` like @hb91. I just happened to open this question again to see if anyone else commented. – Peter Cordes May 24 '22 at 19:13
  • 1
    Do you even have an arduino connected to your Mac? Via USB? Anyway no, a random process you run under MacOS (via Docker) doesn't get snooped by an Arduino no matter how it's connected. If you had an Arduino connected to a Linux system somehow and opened a device file for a driver that exposed its memory over USB, then you could `mmap` that file descriptor into memory of your process and store to those addresses. But that would work by triggering a page fault on each store on the host CPU, which the driver would look at. Or by occasionally syncing like mmap on a regular file. – Peter Cordes May 24 '22 at 19:18
  • 1
    I highly doubt anyone has written a driver like that, because it wouldn't work very well (only page granularity for syncing and no ordering of operations). It would make way more sense to just run code in an arduino emulator, or for remote-IO accesses to the Arduino to be done via system calls for each one, because normally it's important for I/O operations to be ordered wrt. each other! (Which that `mmap` hacky idea wouldn't give unless it catches page faults, which would be just as slow as system calls, or probably slower.) – Peter Cordes May 24 '22 at 19:20

0 Answers0