2

I've been trying to implement a loop into my program however I'm getting confused as to how I would be able to achieve the desired outcome.

I've been practising creating loops like this

(just an example of)

void f()
{
        int a, b ;

        for(a = 10, b = 1; a != 0; --a)
        {
            b = b << 2 ;            
        }       
}

I used the following question to practice with for loops in assembly: For loop in x86 assembly and optimising code?

_f:
        push ebp
        mov ebp, esp

        sub esp, 8                 ; int a,b

initialize:                        ; for
        mov dword ptr [ebp-4], 10  ; a = 10,
        mov dword ptr [ebp-8], 1   ; b = 1

        mov eax, [ebp-4]
condition:      
        test eax, eax              ; tests if a == 0
        je exit

runCode:
        mov eax, [ebp-8]
        shl eax, 2                 ; b = b << 2
        mov dword ptr [ebp-8], eax

modify:
        mov eax, [ebp-4]
        sub eax, 1                 ; --a
        mov dword ptr [ebp-4], eax
        jmp condition

exit:
        mov esp, ebp
        pop ebp
        ret

Now, in my program, I have this for loop:

void encrypt_chars(int length, char EKey)
{
    char temp_char;                         // char temporary store

    for (int i = 0; i < length; i++)        // encrypt characters one at a time
    {
        temp_char = OChars[i];              // temp_char now contains the address values of the individual character
        __asm
        {
            // Assembly code for encryption
        }
        EChars[i] = temp_char;              // Store encrypted char in the encrypted chars array
    }
    return;

I don't fully understand how to take the parameters from the function and implement them into the loop in assembly. Would anyone be able to show me an example of that please?

full code: http://pastebin.com/x14L7e3x

Would anyone be able to show me how to convert the above loop into assembly? How do I write OChars and EChars in assembly also?

thanks.

EDIT: I've had a go at implementing it but I get a few errors when I run my program:

Here's the code:

void encrypt_chars(int length, char EKey)
{
    char temp_char;

    __asm
    {
        mov     dword ptr[ebp - 14h], 0
        jmp     encrypt_chars + 30h
        mov     eax, dword ptr[ebp - 14h]
        add     eax, 1
        mov     dword ptr[ebp - 14h], eax
        mov     eax, dword ptr[ebp - 14h]
        cmp     eax, dword ptr[length] // error here
        jge     encrypt_chars + 6Ah

        mov     eax, dword ptr[ebp - 14h] // the temp_char = OChar[i]
        mov     cl, byte ptr
        mov     byte ptr[temp_char], cl

        push    eax
        push    ecx
        movzx   ecx, byte ptr[temp_char]
        push    ecx
        lea     eax, [EKey]
        push    eax

        call    encrypt4
        add     esp, 8
        mov     byte ptr[temp_char], al
        pop     ecx
        pop     eax


        mov    eax, dword ptr[ebp - 14h]
        mov    cl, byte ptr[temp_char]
        byte   ptr[temp_char], cl // error here


        jmp   encrypt_chars + 27h

    }
    return;

    __asm
    {
    encrypt4:
            push    ebp                 // Set stack
            mov     ebp, esp            // Set up the base pointer

            mov     eax, [ebp + 8]      // Move value of parameter 1 into EAX
            mov     ecx, [ebp + 12]     // Move value of parameter 2 into ECX
            push    edi                 // Used for string and memory array copying
            push    ecx                 // Loop counter for pushing character onto stack

            not     byte ptr[eax]       // Negation
            add     byte ptr[eax], 0x04 // Adds hex 4 to EKey
            movzx   edi, byte ptr[eax]  // Moves value of EKey into EDI using zeroes
            pop     eax                 // Pop the character value from stack
            xor     eax, edi            // XOR character to give encrypted value of source
            pop     edi                 // Pop original address of EDI from the stack

            rol     al, 1               // Rotates the encrypted value of source by 1 bit (left)
            rol     al, 1               // Rotates the encrypted value of source by 1 bit (left) again
            add     al, 0x04            // Adds hex 4 to encrypted value of source

            mov     esp, ebp            // Deallocate values
            pop     ebp                 // Restore the base pointer
            ret
    }

    //--- End of Assembly code
}
// end of encrypt_chars function
Community
  • 1
  • 1
  • Create a minimal example ;-) – Ciro Santilli OurBigBook.com Apr 28 '15 at 09:28
  • @CiroSantilli六四事件法轮功 Sorry, I'm not sure what you mean by that :) –  Apr 28 '15 at 09:41
  • Please see my edit for my code –  Apr 28 '15 at 10:03
  • 3
    @Henrik No, I want to use assembly because I want to learn how to use assembly code... –  Apr 28 '15 at 10:19
  • *I get an error when I run my program*. What error do you get? – lurker Apr 28 '15 at 11:09
  • @lurker http://i.gyazo.com/d36898bbac8894ad57987d2126fbd507.png –  Apr 28 '15 at 11:27
  • Why does your first block of `asm` code do a return sequence (`mov esp, ebp`, `pop ebp`, and `ret`)? – lurker Apr 28 '15 at 11:30
  • From having a quick look at your attempt: No need for `sub esp, 8` if you already declare `i & temp_char` in the c source, if you didn't it would be `5` not `8` i.e `int(4 bytes) + char(1 byte) = 5`. `length` isn't a local variable, it's the first parameter so it's located at `ebp+8` and don't try to set `length` to zero. Don't test if `i` equals zero, `cmp` it to length and jump if below (`jb exit`). Remove the assembly code after the exit label, that is what's called a `function epilogue`, it was there because I converted the entire example **function** in my answer to the other question – James Apr 28 '15 at 11:55
  • @lurker I'm really not sure. I'm extremely confused with this :( –  Apr 28 '15 at 11:58
  • @James I am really sorry for this but could you please show me an example of how to apply this loop in assembly with the reference to the OChars array as well? Please x –  Apr 28 '15 at 12:09
  • I'll see what i can do, but i don't have a clue about the overall logic of your program, if i come up with something for you i'll be sure to post an answer but no promises. (also in my last comment i said to "jump if below" which is a mistake, it should be "jump if above or equal" which looks like this `jae exit`) – James Apr 28 '15 at 12:33
  • @James I've made an alteration to my code, please would you be able to check it out? (See edit) I got the code from using disassembly view in Visual Studio 2013 but I get a few erros such as: http://gyazo.com/3b6875c9e1207df61df4e95506af7ed6 –  Apr 28 '15 at 12:33

0 Answers0