weam
weam

Reputation: 11

Assembly char* linked

I am writing code in C and Assembly x86-64bit, I compile on Ubuntu 16.04 using NASM and gcc.

I have a label in assembly called _sum. I call _sum and send a char* containing number only (for example: "87") and it is received by the register "rdi". When I perform the following:

mov rbx, [rdi]
inc rdi
mov rcx, [rdi]

I notice that register rbx contains the hex value of all the digits linked together, (in the previous example: rbx has 0x3738 and rcx has 0x37). How can I make it that each time [rdi] contains only the value of the current digit (0x38) and not all the digits linked?

Upvotes: 1

Views: 75

Answers (1)

Netch
Netch

Reputation: 4562

If you use 32- or 64-bit move to register, you'll definitely get values of 4 or 8 bytes starting with the pointed byte, not only a requested one. The simplest variant is to use zero padding extension instruction, like:

movzx ebx, byte [rdi]

Note that filling ebx with some value automatically causes clearing of upper 4-byte half of rbx, so, this is the same as calling movzx to rbx. But, sign-extension requires an explicit r** argument.

There are alternatives, like:

xor ebx, ebx
mov bl, [edi]

or:

mov bl, [edi]
and ebx, 0ffh

but all they are less efficient on any modern processor.

Upvotes: 2

Related Questions