1

I have converted a c program into an assembly program and am trying to simplify the code:

.global Func1
Func1: save %sp, -112, %sp
!st %i0, [%fp+68]
!st %i1, [%fp+72]
!ld [%fp+68], %o0
!ld [%fp+72], %o1
call    Func2, 0
nop
mov %o0, %g1
mov %g1, %i0
ret
restore

.global Func2
Func2: save %sp, -112, %sp
!st %i0, [%fp+68]
!st %i1, [%fp+72]
!ld [%fp+68], %g2
!ld [%fp+72], %g1
add %g2, %g1, %g1
umul    %g1, 2, %g1
mov %g1, %i0
ret
restore

I've simplified it enough to the point where when I try to change something it changes the return value of the program. My question is, what does st and ld do? I know what they mean, but what exactly is going on here? Is there a different way to do this by just using mov or something else? It's hard to find resources for this kind of thing online so help would be appreciated.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Dillon Burton
  • 363
  • 6
  • 16
  • Please explain what architecture this is, asn what assembler you are using. (To my untrained eye it looks like SPARC...). How did you "transform C to assembly"? What do you hope to win by this? – vonbrand Jan 30 '13 at 02:57
  • It looks like SPARC, so `sparc` tag added. – nrz Jan 30 '13 at 03:08
  • Sorry, it is SPARC forgot to add that. Thank you. – Dillon Burton Jan 30 '13 at 03:44
  • They are load and store. SPARC does not have a mov instruction, though some assemblers support it as a pseudo-instruction – Chris Dodd Jan 30 '13 at 06:59
  • looks like spark derived from commodore64 – Алексей Неудачин May 26 '21 at 10:20
  • The store and reload are only there because you compiled without optimization. Your incoming args are in regs where you could use them with `add` / `umul` directly. [Why does clang produce inefficient asm with -O0 (for this simple floating point sum)?](https://stackoverflow.com/q/53366394) – Peter Cordes May 26 '21 at 18:20

1 Answers1

2

Sparc has a load-store architecture unlike for example x86 and similar to most RISC cpu architectures such as PPC,MIPS and ARM.

ld and st are the only real (as in implemented in hardware) instructions that move data to and from main memory. They are also used to access hardware that presents itself as an in memory interface.

syntax for them is

ld register_name , memory_location the same syntax goes for st.

remember you can only use ld and st on values in memory ... and all the rest apply to the register file. If you haven't loaded it yet it isn't usable and if you don't ever store it it won't be in memory (well untill you overwrite it in the register file its still there).

If you need a book on Sparc ASM see this "Fundamentals of Computer Organization and Design" you can view parts of it on google books. Also do note that x86 (CISC) asm is very diffferent from RISC-like asm. That said which is relevant to you depends if you are more of a pure software person Sparc is fine (it will be similar to many types of compiler intermediate representations) but if you are interested in fine tuning high performance applications you might rather study x86 asm.

cb88
  • 447
  • 3
  • 18