0

What can I do to take only the right digits of the num ? I tried byte ptr but it changed the vale. Is it possible to delete the 2 left digits in a register?

edit: the number is written within a register so by right and left i meant the high order byte(left) and low order byte(right)

00 00

for example if I have 1203 i want to turn it into 0003 but it still has to be word sized

mov bx, [bp+6]
mov al, [byte ptr bx]
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • You're working with a number or a string? If it's a number I suggest you check how numbers are represented in binary. – m0skit0 Dec 22 '21 at 08:53
  • I have trouble understanding your question. Could you perhaps [edit] your question and give some sample input and desired output? – fuz Dec 22 '21 at 11:21
  • "From word to byte" sounds like narrowing. My best guess from the question body is that you actually want to zero-extend a byte into a 16-bit (word) register. To make AX = (uint8_t)AL. – Peter Cordes Dec 23 '21 at 05:15

1 Answers1

1

Try clearing the high order byte. For example, try

mov al, [byte ptr bx] ; load low order byte
xor ah, ah            ; clear high order byte

On the 80386 or later, you can also use the movzx instruction:

movzx ax, [byte ptr bx]
fuz
  • 88,405
  • 25
  • 200
  • 352
  • 1
    Generally prefer `xor ax,ax` / `mov al, [bx]` to do a zero-extending load without MOVZX, unless you want to keep AH around until some time after the actual load. That might possibly have some partial-register advantages on some modern CPUs, and is the same length. Although word xor-zeroing isn't dep-breaking, so that may just be making the dep chain longer! So maybe only good in terms of converting the same idiom to 32-bit... (I omitted `byte ptr` because its implied by the destination, and it's weird to see it inside the brackets. I didn't know that was valid in any assemblers.) – Peter Cordes Dec 23 '21 at 05:10
  • 1
    Fun fact: [on Haswell/Skylake](https://stackoverflow.com/questions/45660139/how-exactly-do-partial-registers-on-haswell-skylake-perform-writing-al-seems-to), `mov ah, 0` is more efficient than `xor ah,ah` (no false dependency), and also the same 2-byte code size, but probably not worth trying to get into that complexity and just encourage xor of full registers for zero extension. – Peter Cordes Dec 23 '21 at 05:13