2

How do you get the equivalent of this C with SPARC assembly:

printf( "Hello, my name is %s.\n", name );

using the function prototype:

void printName( const char* msg, const char* name )

where msg is "Hello, my name is %s.\n".

I know that I can define "Hello, my name is %s.\n" in the data segment with .asciz without having the first argument msg, but is there a way to pass a string into an assembly function that would have a %s identifier in it? Can a char* even take in a format identifier? I've tried the following but I get a core dump.

Function call in C:

char * msg = "Hello, my name is %s.\n";
char * name = "Foo";

printName( msg, name );

Assembly:

mov %i0, %o0
mov %i1, %o1
call printf, 2
nop

Maybe I'm not approaching the function prototype correctly?

2
  • 1
    I don't understand the question. Does that C code snippet work? If so, why not just take a look at the assembler that has been generated by the compiler? Apr 28, 2011 at 7:08
  • Oli is right, looking at the generated assembler is going to be your easiest approach. I don't know sparc assembler, but you'll probably find that arguments to C functions are passed on the stack, not as registers (which is what it looks like you're doing). If you haven't already, I'd start with trying to get 'printf("hello world\n");' to work first, i.e. no argument specifiers, then work from there.
    – forsvarir
    Apr 28, 2011 at 14:46

2 Answers 2

2

I am actually not quiet sure what you are doing wrong, but the following program works as it should:

        .data
s0:     .asciz  "foo %s\n"
s1:     .asciz  "bar"
        .text
        .global main
main:
        save    %sp, -96, %sp
        set     s0, %o0
        set     s1, %o1
        call    prtnam
        nop
        ret
        restore
prtnam:
        save    %sp, -96, %sp
        mov     %i0, %o0
        call    printf
        mov     %i1, %o1
        ret
        restore

Regarding the passing of arguments, a small number of arguments like this are passed in registers.

2

printf, since it takes variable arguments, takes the unnamed arguments on the stack, and not in registers. That is, the named 'format' argument will be passed in the first argument register, but subsequent arguments will be passed on the stack.

See the section 'Variable Argument List' in the 'Code Examples' section of the SPARC ABI.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.