Questions tagged [sign-extension]

72 questions
31
votes
7 answers

Bit wise '&' with signed vs unsigned operand

I faced an interesting scenario in which I got different results depending on the right operand type, and I can't really understand the reason for it. Here is the minimal code: #include #include int main() { uint16_t check…
Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
19
votes
2 answers

Is a sign or zero extension required when adding a 32bit offset to a pointer for the x86-64 ABI?

Summary: I was looking at assembly code to guide my optimizations and see lots of sign or zero extensions when adding int32 to a pointer. void Test(int *out, int offset) { out[offset] = 1; } ------------------------------------- movslq %esi,…
Yale Zhang
  • 1,447
  • 12
  • 30
15
votes
1 answer

Clarifications on signed/unsigned load and store instructions (MIPS)

I can't seem to grasp the concept on these stuff, even with the help of Google and a textbook in my hand. Following the format (opcode, rs, rt, offset)... Do you sign extend the offset before adding it to the value of the address? Or add before…
Check
  • 153
  • 1
  • 1
  • 4
13
votes
1 answer

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

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?
user5469576
12
votes
1 answer

What does movslq do?

I have trouble finding out what movslq instruction does. Google isn't very helpful and there is no such instruction on this list. Here I have read that MOVSLQ is move and sign-extend a value from a 32-bit source to a 64-bit destination. I…
ibodi
  • 1,543
  • 3
  • 21
  • 40
8
votes
1 answer

assembly cltq and movslq difference

Chapter 3 of Computer Systems A Programmer's Perspective (2nd Edition) mentions that cltq is equivalent to movslq %eax, %rax. Why did they create a new instruction (cltq) instead of just using movslq %eax,%rax? Isn't that redundant?
lisency
  • 433
  • 1
  • 5
  • 9
7
votes
6 answers

Which real use cases exist for arithmetic right bit shifting?

I stumbled upon a question that asks whether you ever had to use bit shifting in real projects. I have used bit shifts quite extensively in many projects, however, I never had to use arithmetic bit shifting, i.e., bit shifting where the left operand…
gexicide
  • 38,535
  • 21
  • 92
  • 152
6
votes
7 answers

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

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…
Beto
  • 806
  • 3
  • 12
  • 33
5
votes
1 answer

Assembly Language: cbw

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?
raphnguyen
  • 3,565
  • 18
  • 56
  • 74
5
votes
1 answer

Weird results with movzwl, %ax and negative values

Alright, so I am dealing with the following snippet of code: push %ebp mov %esp,%ebp push %ebx mov 0x8(%ebp),%eax movzwl %ax,%edx So this behaves as expected when dealing with positive values. The value copied into %edx is the…
ebensing
  • 6,409
  • 4
  • 18
  • 20
5
votes
1 answer

Java Bitwise Or Between Byte and Int

I am trying to perform a bitwise or on a byte value I have in Java. For example, I am running: byte b = (byte)0b11111111; int result = 0 | b; My expected result for this would be 0b00000000 00000000 00000000 11111111, or 255. However, I am…
5
votes
2 answers

Shifting the Sign extended constant in MIPS

Why do we shift by 2 the sign extended 16bit constant in branching instruction in MIPS? I am confused with this idea. What good does this shifting brings to the sign extended 16 bit constant. Here is the picture: Regards
Alfred
  • 1,543
  • 7
  • 33
  • 45
4
votes
1 answer

What do the `uxtx` and `sxtx` extensions mean for 32-bit AArch64 `adds` instruction?

I'm looking at the following disassembled AArch64 instruction: 65 6E 20 2B adds w5, w19, w0, uxtx #3 According to the ARM manual, uxtx zero-extends w0 to an unsigned 64-bit value before adding it to the value in w19. But w19 is a 32-bit "slice"…
John Källén
  • 7,551
  • 31
  • 64
4
votes
2 answers

How to safely extract a signed field from a uint32_t into a signed number (int or uint32_t)

I have a project in which I am getting a vector of 32-bit ARM instructions, and a part of the instructions (offset values) needs to be read as signed (two's complement) numbers instead of unsigned numbers. I used a uint32_t vector because all the…
4
votes
6 answers

Signed right shift = strange result?

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…
identity
  • 949
  • 6
  • 14
1
2 3 4 5