Wednesday 15 January 2014

How do I add 2 unsigned 1 byte numbers in Intel assembly? -


I'm just beginning to learn the writing of assembly of autodidactic assembly.

How can I add two 1 byte integers by using Linux on X86_64?

One way to learn asembly is to write what you want to do in a processor, such as a high language, C, and then see how the compiler generates.

  Unsigned four additional (unsigned char, unsigned char b) {return a + b; }  

Sometimes it leads to some weird but valid, assembler build:

  movzbl-4 (% rbp),% edx; A movzbl-8 (% Rbp),% eax; B leal (% rdx,% rax),% eax; A + b  back 

This time, the LEA is the command, instead of the expected ADD instructions, two 8 stored in 64 bit registers The key used to add bit numbers is that these registers have been initialized using the MOVZX command, which fills all unused bits (up to bit 31) with 0. This example is written using Intel syntax, it is as follows:

  movzx eax, [rbp-8]; A - & gt; Ax MJXX AdX, [RBP-4]; B - & gt; Adx lia ax, [rx + rdx]; A + B - & gt; Eax  

Nobody can think that adding 64 bit registers is strange, when only 32 bits have been started. Keeping in mind that using 8 to 31 bits using the MOVZX directive has started with 0, whatever happens with the upper half of RAX is not important In the lower 8 bit of RAC, we have 8 extra results (which may or may not overflow)

Visual Studio, when no optimization is enabled, more "predecessor" version assembler Version generates this function, Which you can study with your original C code:

Note: This is compiled using 32 bit code generation.

  Unsigned four additional (unsigned char A, unsigned char b) {00EA10A0 push EBP 00 EA 10 10 A1 MOP EBP, ASP return A + B; 00EA10A3 movzx eax, bte ptr [ebp + 8] 00EA10A7 movzx ecx, byte ptr [ebp + 0Ch] 00EA10AB add eX, ecx} 00EA10AD pop ebp 00EA10AE ret  

Both compilers use it Try full-sized registers, even though some of them have a real effect, and due to this the cost of both the 8 V is ended using MOVZX instructions to load into a huge register. Use signed characters and you will see what happens.


No comments:

Post a Comment