简体   繁体   English

MSP430 中的 sxt 指令到底有什么作用?

[英]What does sxt instruction in MSP430 really do?

I understand that sxt is sign extend instruction.我知道sxt是符号扩展指令。 But I don't understand how does this really work.但我不明白这是如何真正起作用的。 For example if my r15 register stores byte 0045 , then what sxt r15 would do to it?例如,如果我的r15寄存器存储字节0045 ,那么sxt r15会对它做什么?

I've been working on problems that use this instruction a lot.我一直在处理大量使用此指令的问题。 To compress it down to a single function, I would write it (using Pythonic syntax) as:要将其压缩为单个函数,我会将它(使用 Pythonic 语法)编写为:

def sxt(x):
    if x % 256 > 127:    # if top bit of low byte is set
        x |= 0xff00
    else:
        x &= 0x00ff

This could be written more compactly, and possibly more efficiently.这可以写得更紧凑,也可能更有效。 This version is intended to be most intuitive for non-asm-natives.此版本旨在对非 asm 本地人最直观。

Note also that the function assumes you pass inputs in as two byte words like the sxt instruction operates on, 0xhhhh where h is a hex digit, which you can do in Python.另请注意,该函数假设您将输入作为两个字节的字传入,例如sxt指令所操作的0xhhhh ,其中 h 是十六进制数字,您可以在 Python 中执行此操作。

The sxt instruction sign extends a byte into a 16 bit word, ie it copies bit 7 into bits 8 to 15 and then updates the flags appropriately. sxt指令符号将一个字节扩展为一个 16 位字,即将第 7 位复制到第 8 到 15 位,然后适当地更新标志。

For your example, sxt would have no effect as the byte 0045 does not have its most significant bit set, so the high byte of r15 stays at zero.对于您的示例, sxt将不起作用,因为字节0045没有设置其最高有效位,因此r15的高字节保持为零。

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

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