-2

I am trying to convert the following C program to inline assembly:

#include <stdio.h>
#include <string.h>

int main()
{
    int counter = 0;
    int input = 0;
    char name[20];



    printf("Please input an integer value 1 - 99: ");
    scanf("%d", &input);
    printf("You entered: %d\n", input);

    if (input <= 0)
    {
        printf("Error, invalid input \n");
    }

    if (input > 0)
    {
        printf("Please input name \n");
        scanf("%s", name);

        for (int i = 1; i <=input; i++)
        {
            printf("Your name is %s\n", name);
            printf("%d\n", i);

        }
    }

    return 0;
}

Here is my attempt at the inline:

.LC0:

.string "please input an interger value 1-99"

.LC1:

.string "%d"

.LC2:

.string "you entered %d"

.LC3:

.string "Error invalid input"

.LC4:

.string "please input name"

.LC5:

.string "%s"

.LC6:

.string "Your name is %s"

main:

push rbp

mov rbp, rsp

sub rsp, 32

mov DWORD PTR [rbp-8], 0

mov DWORD PTR [rbp-12], 0

mov edi, OFFSET FLAT:.LC0

mov eax, 0

call printf

lea rax, [rbp-12]

mov rsi, rax

mov edi, OFFSET FLAT:.LC1

mov eax, 0

call scanf

mov eax, DWORD PTR [rbp-12]

mov esi, eax

mov edi, OFFSET FLAT:.LC2

mov eax, 0

call printf

mov eax, DWORD PTR [rbp-12]

test eax, eax

jg .L2

mov edi, OFFSET FLAT:.LC3

mov eax, 0

call printf

.L2:

mov eax, DWORD PTR [rbp-12]

test eax, eax

jle .L3

mov edi, OFFSET FLAT:.LC4

mov eax, 0

call printf

lea rax, [rbp-32]

mov rsi, rax

mov edi, OFFSET FLAT:.LC5

mov eax, 0

call scanf

mov DWORD PTR [rbp-4], 1

jmp .L4

.L5:

lea rax, [rbp-32]

mov rsi, rax

mov edi, OFFSET FLAT:.LC6

mov eax, 0

call printf

mov eax, DWORD PTR [rbp-4]

mov esi, eax

mov edi, OFFSET FLAT:.LC1

mov eax, 0

call printf

add DWORD PTR [rbp-4], 1

.L4:

mov eax, DWORD PTR [rbp-12]

cmp DWORD PTR [rbp-4], eax

jle .L5

.L3:

mov eax, 0

leave

ret

So I guess my question is, am I on the right track? I find this stuff extremely confusing. It doesn't help that all my instructor sais is that "I am on the right track" so any help would be much appreciated! (Whenever I run this I get an error on the first line complaining about the ".")

John H
  • 27
  • 6
  • 1
    When you're talking about errors on Stack Overflow, please, **please** include the exact text of the error in your question. Don't leave us guessing. It's also helpful to indicate what assembler you're using so we can try and reproduce the problem, and when it comes to assembly to specify the specific ISA and OS you're going to target. – tadman Mar 29 '18 at 20:46
  • 1
    To be clear, what you have there is not **inline** assembly. [Inline assembly](https://en.wikipedia.org/wiki/Inline_assembler) is just a few lines (typically) of assembly in a C function. What you have is a program written in assembly. If you are attempting to compile that with a C compiler, then yeah, the first thing the C compiler is going to complain about is the "." – user3386109 Mar 29 '18 at 21:02
  • @user3386109 If it's the same syntax as the assembler the C compiler uses, one _can_ say: `cc -c foo.s`. The `.Lx` labels (e.g. `.L3`) are a strong indication that the `.s` file was generated from `cc -S foo.c` – Craig Estey Mar 29 '18 at 21:21
  • @CraigEstey And that may be the answer OP is looking for: the filename needs to end with `.s`, not `.c` – user3386109 Mar 29 '18 at 21:23
  • Use `gcc -S -masm=intel -Os foo.c` if you want asm that doesn't suck. (`-Os` optimizes for size, instead of making noisy / clunky un-optimized code). Of course, this is almost all just function calls, so there's not much here. It only looks like a giant mess because you double-spaced it and broke the indenting the compiler uses. Related: https://stackoverflow.com/questions/38552116/how-to-remove-noise-from-gcc-clang-assembly-output – Peter Cordes Mar 30 '18 at 02:34

1 Answers1

0

Thank you guys! Figured out that the inline was unnecessary and that we were supposed to be doing regular assembly! Really appreciate the help!

John H
  • 27
  • 6