sign-extension

Assembly Language: cbw

拥有回忆 提交于 2019-12-10 17:21:03
问题 I am unsure of what the cbw command actually does. I have a snippet of code: mov ax,0FF0h cbw idiv ah How does the value of ax change after cbw? 回答1: The cbw instruction sign-extends a byte into a word. In this case, it'll take the sign bit of AL (which happens to be 1) and copy it into every bit of AH . This means that the two's-complement value of AX will be the same, but the binary representation will be different. The value of AX after the cbw instruction will be FFF0h (a 16-bit -16 value

Bit extension occuring when trying to store a “byte” of information into a %x

不打扰是莪最后的温柔 提交于 2019-12-10 11:46:01
问题 So I currently need to read in a string of hex ASCII characters and use them to determine a specific opcode. To do so I open a text file(its not really but for simple explanation we'll call it that) then read in the line. So if I get a line like 40f1... I have the function read the 2 characters and store them as an unsigned int into mem_byte. Then I cast it into a char to use as an array and retain the "byte" worth of information or the numerical value of two hex digits which were obtained by

Bit extension occuring when trying to store a “byte” of information into a %x

末鹿安然 提交于 2019-12-08 05:04:26
So I currently need to read in a string of hex ASCII characters and use them to determine a specific opcode. To do so I open a text file(its not really but for simple explanation we'll call it that) then read in the line. So if I get a line like 40f1... I have the function read the 2 characters and store them as an unsigned int into mem_byte. Then I cast it into a char to use as an array and retain the "byte" worth of information or the numerical value of two hex digits which were obtained by reading in ASCII character representation of 2 hex digits. void getInstructions(FILE* read_file, int*

Zero/sign-extend are no-op, why then instructions for each size type?

好久不见. 提交于 2019-12-06 06:51:29
For x86 and x64 compilers generate similar zero/sign extend MOVSX and MOVZX. The expansion itself is not free, but allows processors to perform out-of-order magic speed up. But on RISC-V: Consequently, conversion between unsigned and signed 32-bit integers is a no-op, as is conversion from a signed 32-bit integer to a signed 64-bit integer. A few new instructions (ADD[I]W/SUBW/SxxW) are required for addition and shifts to ensure reasonable performance for 32-bit values. (C) RISC-V Spec But at the same time, the new modern RISC-V 64-bit processors contains instructions for 32-bit signed

What does “extend immediate to 32 bits” mean in MIPS?

你离开我真会死。 提交于 2019-12-02 10:10:41
I'm reading about the Instruction Decode (ID) phase in the MIPS datapath, and I've got the following quote: "Once operands are known, read the actual data (from registers) or extend the data to 32 bits (immediates)." Can someone explain what the "extend the data to 32 bits (immediates)" part means? I know that registers all contain 32 bits, and I know what an immediate is. I just don't understand why you need to extend the immediate from 26 to 32 bits. Thanks! On a 32-bit CPU, most of the operations you do (like adding, subtracting, dereferencing a pointer) are done with 32-bit numbers. When

When and why do we sign extend and use cdq with mul/div?

帅比萌擦擦* 提交于 2019-11-30 05:49:27
问题 I had a test todayand the only question I didn't understand involved converting a doubleword to a quad word. That got me thinking, why/when do we sign extend for multiplication or division? In addition, when do we use instructions like cdq? 回答1: Use cdq / idiv for signed 32-bit / 32-bit => 32 bit division, xor edx,edx / div for unsigned. If you zero EDX/RDX instead of sign-extending into EDX:EAX before idiv , you can get a large positive result for -5 / 2, for example. Using the "full power"

Signed right shift = strange result?

走远了吗. 提交于 2019-11-29 14:38:43
I was helping someone with their homework and ran into this strange issue. The problem is to write a function that reverses the order of bytes of a signed integer(That's how the function was specified anyway), and this is the solution I came up with: int reverse(int x) { int reversed = 0; reversed = (x & (0xFF << 24)) >> 24; reversed |= (x & (0xFF << 16)) >> 8; reversed |= (x & (0xFF << 8)) << 8; reversed |= (x & 0xFF) << 24; return reversed; } If you pass 0xFF000000 to this function, the first assignment will result in 0xFFFFFFFF . I don't really understand what is going on, but I know it has

Signed extension from 24 bit to 32 bit in C++

纵饮孤独 提交于 2019-11-29 14:32:57
I have 3 unsigned bytes that are coming over the wire separately. [byte1, byte2, byte3] I need to convert these to a signed 32-bit value but I am not quite sure how to handle the sign of the negative values. I thought of copying the bytes to the upper 3 bytes in the int32 and then shifting everything to the right but I read this may have unexpected behavior. Is there an easier way to handle this? The representation is using two's complement. You could use: uint32_t sign_extend_24_32(uint32_t x) { const int bits = 24; uint32_t m = 1u << (bits - 1); return (x ^ m) - m; } This works because: if

Signed right shift = strange result?

独自空忆成欢 提交于 2019-11-28 08:36:08
问题 I was helping someone with their homework and ran into this strange issue. The problem is to write a function that reverses the order of bytes of a signed integer(That's how the function was specified anyway), and this is the solution I came up with: int reverse(int x) { int reversed = 0; reversed = (x & (0xFF << 24)) >> 24; reversed |= (x & (0xFF << 16)) >> 8; reversed |= (x & (0xFF << 8)) << 8; reversed |= (x & 0xFF) << 24; return reversed; } If you pass 0xFF000000 to this function, the

Signed extension from 24 bit to 32 bit in C++

a 夏天 提交于 2019-11-28 08:20:41
问题 I have 3 unsigned bytes that are coming over the wire separately. [byte1, byte2, byte3] I need to convert these to a signed 32-bit value but I am not quite sure how to handle the sign of the negative values. I thought of copying the bytes to the upper 3 bytes in the int32 and then shifting everything to the right but I read this may have unexpected behavior. Is there an easier way to handle this? The representation is using two's complement. 回答1: You could use: uint32_t sign_extend_24_32