Marvee Amasi
Marvee Amasi
DIIDevHeads IoT Integration Server
Created by Marvee Amasi on 6/28/2024 in #📦-middleware-and-os
Trying to access the sensor's registers using memory-mapped I/O in a flat memory model
No more error message, it worked:
global _start

section .text
_start:
; I2C communication registers set up...

;
mov $0, %eax ; opening system call
mov $0xFD, %ebx ; open file descriptor for /dev/mem
mov $0, %ecx ;
mov $_dev_mem, %edx ;
int $0x80 ; system call

; checking for successful open
cmp $0, %eax
jl .open_failed ; if fails

; Mapping my memory region
mov $9, %eax ; mmap system call
mov %eax, %ebx ; file descriptor from open
mov $0x1000, %ecx ;
mov $0x1000, %edx ;
mov $0x1, %esi ; prot - PROT_READ
mov $0x0, %edi ; flags
mov $0, %ebp ; offset
int $0x80 ; system call

; if mapping is successful
cmp $0, %eax
jl .mmap_failed ; if mapping fails

; accessing registers using the mapped address
movb (%rax), %bl ; reading byte from temp register

; processing my temp data...

; Unmapping memory region before exiting
mov $17, %eax ; munmap system call
mov %eax, %ebx ; mapped address
mov $0x1000, %ecx ;
int $0x80 ; system call

mov $1, %eax
int $0x80 ; exit system call

.open_failed:
; Handle error message
...

.mmap_failed:
; Handle mapping error
...

section .data
_dev_mem: db "/dev/mem", 0x0 ;
message db "Temp data read", 0x0
global _start

section .text
_start:
; I2C communication registers set up...

;
mov $0, %eax ; opening system call
mov $0xFD, %ebx ; open file descriptor for /dev/mem
mov $0, %ecx ;
mov $_dev_mem, %edx ;
int $0x80 ; system call

; checking for successful open
cmp $0, %eax
jl .open_failed ; if fails

; Mapping my memory region
mov $9, %eax ; mmap system call
mov %eax, %ebx ; file descriptor from open
mov $0x1000, %ecx ;
mov $0x1000, %edx ;
mov $0x1, %esi ; prot - PROT_READ
mov $0x0, %edi ; flags
mov $0, %ebp ; offset
int $0x80 ; system call

; if mapping is successful
cmp $0, %eax
jl .mmap_failed ; if mapping fails

; accessing registers using the mapped address
movb (%rax), %bl ; reading byte from temp register

; processing my temp data...

; Unmapping memory region before exiting
mov $17, %eax ; munmap system call
mov %eax, %ebx ; mapped address
mov $0x1000, %ecx ;
int $0x80 ; system call

mov $1, %eax
int $0x80 ; exit system call

.open_failed:
; Handle error message
...

.mmap_failed:
; Handle mapping error
...

section .data
_dev_mem: db "/dev/mem", 0x0 ;
message db "Temp data read", 0x0
Had to dig deep with some resources using mmap
11 replies