简体   繁体   English

从32位寄存器读取16位

[英]Reading in 16 bits from a 32 bit register

I'm trying to read certain values from a particular register. 我正在尝试从特定寄存器中读取某些值。 The manual specifies that I must access the 16-bit LSB access first and 16-bit MSB access next. 该手册指定我必须首先访问16位LSB访问,然后访问16位MSB访问。 Do I just read in all 32 bits at once and then mask the remaining 16 msb/lsb respectively as needed? 我是否只是一次读取所有32位,然后根据需要分别屏蔽剩余的16 msb / lsb? Or would there be a way to read only 16 bits fist. 或者有没有办法只读16位拳。

Thanks, Neco 谢谢,Neco

If the manual says to first access the 16-bit LSB and then the 16-bit MSB, do as the manual says. 如果手册说要首先访问16位LSB然后再访问16位MSB,请按照手册中的说明进行操作。

For example (little endian): 例如(小端):

#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);

Note that compilers also sometimes provide HI and LO identifiers to access LSB or MSB, like in addition to REG in the example: 请注意,编译器有时也会提供HI和LO标识符来访问LSB或MSB,就像示例中的REG一样:

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

It's unclear what language you are using to do this. 目前还不清楚你用什么语言来做这件事。 I will assume you are using inline assembly in C. 我假设您在C中使用内联汇编。

I am most familiar with NASM. 我最熟悉NASM。 Using NASM syntax for i386: 使用i386的NASM语法:

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

I'm guessing the gas (C) code would be something like this: 我猜气体(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM