Read 16 bits from a 32-bit register

I am trying to read specific values ​​from a specific register. The manual states that I should be able to access LSB 16-bit access and 16-bit MSB access. I just read all 32 bits at once, and then masked the remaining 16 msb / lsb accordingly as needed? Or there would be a way to read only 16 bits with your fist.

Thanks Neco

+5
source share
2 answers

If the manual talks about first accessing the 16-bit LSB and then the 16-bit MSB, follow the instructions in the manual.

For example (small end):

#define REG (*(volatile uint32_t *) 0x1234)

uint16_t val_hi, val_lo;

val_lo = *((volatile uint16_t *) &REG);
val_hi = *((volatile uint16_t *) &REG + 1);

, HI LO LSB MSB, , REG :

#define REGL (*(volatile uint16_t *) 0x1234)
#define REGH (*(volatile uint16_t *) 0x1236)
+8

, . , C.

NASM. NASM i386:

mov eax, 0x12345678 ; load whatever value
mov bx, ax          ; put LSW in bx
shr eax, 16         ; shift MSW to ax
                    ; now ax = MSW, bx = LSW

, (C) :

movl $0x12345678, %eax # load whatever value
movw %ax, %bx          # put LSW in bx
shrl $16, %eax         # shift MSW to ax
                       # now ax = MSW, bx = LSW
+2

All Articles