Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add CH to EAX in x86 assembly?

I need to add the contents of CH to EAX in x86 assembly, but there is no address mode that appears to support this. Ideally I would want an addressing mode like:

ADD EAX,r8

or

ADD r32,r8

or

ADD r/m32,r8

But ADD does not have any of these modes. I can't mask ECX because it has other junk in it that I use elsewhere, and I have used up all my other registers, so my only option appears to be to use a memory access. Any ideas how I can solve this problem?

Note I can't use a mode like r/m8,r8 because then there will be no carry.

like image 826
Tyler Durden Avatar asked Jan 13 '23 18:01

Tyler Durden


2 Answers

x86 just doesn't have such flexible addressing modes, as you've observed. You can't add an 8-bit register to a 32-bit register in a single step. Your options are either to free up a register and zero/sign extend then add r32,r32, or to add r8,r8 then branch on the carry flag to adjust the result.

I'd suggest you should spill a register to memory, on a modern processor a pair of memory accesses are much cheaper than a branch (as it will load from the store buffer), and you can probably reword your other code around the spill.

like image 71
jleahy Avatar answered Jan 17 '23 12:01

jleahy


Use a mode like r/m8,r8 and propagate the carry if necessary by adding a constant 0x100 to EAX.

like image 21
Doug Currie Avatar answered Jan 17 '23 13:01

Doug Currie