kabeersvohra
kabeersvohra

Reputation: 1059

Converting into Intel 64 assembly

I have a snippet of code that I need to convert into commented Intel 64 assembly. I have given it a shot but I know that I have made some errors so I would appreciate if someone could point these errors out for me and tell me the correct way of doing it.

Code to be converted:

void test (int x)
{
    int y, z;
    y = 5;
    z = 2*y;
    x = z+x;
    if (z>0)
        z = -z;
    while (z<0) {
        y = y+y;
        z++;
    }
}

int main()
{
    int x = 8;
    test (x);
}

My attempt at conversion:

test:       pop rax; x variable from stack
            mov rbx 5; y variable
            mov rcx rbx; z variable
            imul rcx 2; z = z*y
            add rdx rcx; x = z+x

ifz:        cmp rcx 0; if statement
            jle whilez;
            imul rcx -1; z = -z

whilez:     cmp rcx 0; while statement
            jge endwhile;
            add rbx rbx; y=y+y
            add rcx 1; z++
            jmp whilez; loop back

endwhile:   

main:       mov rax 8; int x = 8
            push rax; push x onto stack for method call
            jmp test;

Upvotes: 1

Views: 98

Answers (1)

CMilby
CMilby

Reputation: 644

What errors are you getting while compiling/linking this?

Right off the bat I notice you have some syntax errors. For Intel-Based assembly the syntax for mov's are...

mov rax, rbx     ; You forgot the comma
add rcx, 8       ; Just another example

This is also the case for the 'imul', 'add', and 'cmp' calls.

Also while not technically incorrect, you add semicolons to the end of lines. In Assembly semicolons are not necessary at the end and are instead the identifier for comments. The commas were the major issue I noticed. I would try to compile and link that and if it still doesn't work I can take another look.

Upvotes: 1

Related Questions