-1

I have converted a c code into mips32 using gcc compiler but there are some parts that I did not understand.

This is c code:

int main()
{
float fibSquared;
int F[10] = {0};
F[0] = 0;
F[1] = 1;
for(int i = 2; i < 10 ; i++)
F[i] = F[i-1] + F[i-2];
fibSquared = sqrt(F[9]);
printf("%f",fibSquared);
return 0;
}

This is mips code:

.file   1 ""
    .section .mdebug.abi32
    .previous
    .nan    legacy
    .module fp=32
    .module nooddspreg
    .abicalls
    .rdata
    .align  2
$LC0:
    .ascii  "%f\000"
    .text
    .align  2
    .globl  main
    .set    nomips16
    .set    nomicromips
    .ent    main
    .type   main, @function
main:
    .frame  $fp,80,$31      # vars= 48, regs= 2/0, args= 16, gp= 8
    .mask   0xc0000000,-4
    .fmask  0x00000000,0
    .set    noreorder
    .cpload $25
    .set    nomacro
    addiu   $sp,$sp,-80
    sw  $31,76($sp)
    sw  $fp,72($sp)
    move    $fp,$sp
    .cprestore  16
    movz    $31,$31,$0
    sw  $0,32($fp)
    sw  $0,36($fp)
    sw  $0,40($fp)
    sw  $0,44($fp)
    sw  $0,48($fp)
    sw  $0,52($fp)
    sw  $0,56($fp)
    sw  $0,60($fp)
    sw  $0,64($fp)
    sw  $0,68($fp)
    sw  $0,32($fp)
    li  $2,1            # 0x1
    sw  $2,36($fp)
    li  $2,2            # 0x2
    sw  $2,24($fp)
    b   $L2
    nop

$L3:
    lw  $2,24($fp)
    nop
    addiu   $2,$2,-1
    sll $2,$2,2
    addiu   $3,$fp,24
    addu    $2,$3,$2
    lw  $3,8($2)
    lw  $2,24($fp)
    nop
    addiu   $2,$2,-2
    sll $2,$2,2
    addiu   $4,$fp,24
    addu    $2,$4,$2
    lw  $2,8($2)
    nop
    addu    $3,$3,$2
    lw  $2,24($fp)
    nop
    sll $2,$2,2
    addiu   $4,$fp,24
    addu    $2,$4,$2
    sw  $3,8($2)
    lw  $2,24($fp)
    nop
    addiu   $2,$2,1
    sw  $2,24($fp)
$L2:
    lw  $2,24($fp)
    nop
    slt $2,$2,10
    bne $2,$0,$L3
    nop

    lw  $2,68($fp)
    nop
    mtc1    $2,$f0
    nop
    cvt.d.w $f0,$f0
    mov.d   $f12,$f0
    lw  $2,%call16(sqrt)($28)
    nop
    move    $25,$2
    .reloc  1f,R_MIPS_JALR,sqrt
1:  jalr    $25
    nop

    lw  $28,16($fp)
    cvt.s.d $f0,$f0
    swc1    $f0,28($fp)
    lwc1    $f0,28($fp)
    nop
    cvt.d.s $f0,$f0
    mfc1    $7,$f0
    mfc1    $6,$f1
    lw  $2,%got($LC0)($28)
    nop
    addiu   $4,$2,%lo($LC0)
    lw  $2,%call16(printf)($28)
    nop
    move    $25,$2
    .reloc  1f,R_MIPS_JALR,printf
1:  jalr    $25
    nop

    lw  $28,16($fp)
    move    $2,$0
    move    $sp,$fp
    lw  $31,76($sp)
    lw  $fp,72($sp)
    addiu   $sp,$sp,80
    j   $31
    nop

    .set    macro
    .set    reorder
    .end    main
    .size   main, .-main
    .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609"

This is the part I could not understand:

    lw  $2,68($fp)
    nop
    mtc1    $2,$f0
    nop
    cvt.d.w $f0,$f0
    mov.d   $f12,$f0
    lw  $2,%call16(sqrt)($28)
    nop
    move    $25,$2
    .reloc  1f,R_MIPS_JALR,sqrt
1:  jalr    $25
    nop

    lw  $28,16($fp)
    cvt.s.d $f0,$f0
    swc1    $f0,28($fp)
    lwc1    $f0,28($fp)
    nop
    cvt.d.s $f0,$f0
    mfc1    $7,$f0
    mfc1    $6,$f1
    lw  $2,%got($LC0)($28)
    nop
    addiu   $4,$2,%lo($LC0)
    lw  $2,%call16(printf)($28)
    nop
    move    $25,$2
    .reloc  1f,R_MIPS_JALR,printf
1:  jalr    $25
    nop

    lw  $28,16($fp)
    move    $2,$0
    move    $sp,$fp
    lw  $31,76($sp)
    lw  $fp,72($sp)
    addiu   $sp,$sp,80
    j   $31
    nop

    .set    macro
    .set    reorder
    .end    main
    .size   main, .-main
    .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609"
  1. What does .reloc mean?
  2. How does %call16 work?
  3. What is the corresponding value of the variable fibSquared?
  4. How the functions "printf" and "sqrt" are called and executed?
GAM
  • 57
  • 1
  • 4
  • 1
    1. https://sourceware.org/binutils/docs-2.28/as/Reloc.html#Reloc 2. https://stackoverflow.com/questions/10175962/assembly-mips-call16printf – Michael Jun 26 '20 at 16:32
  • Please read [ask]. Multi-question posts, do not make for good threads on SO. – jwdonahue Jun 26 '20 at 17:04

1 Answers1

0

I think you take the same class I do because I have the same homework, so I am going to share what I understood myself, it may help.

For %call16, if you noticed between the brackets, the name that the C code uses from the included files such as printf from stdio.h, sqrt from math file, so it's clearly calling them so he can use them.

About the .reloc, I have noticed that they come after %call16 and the called function executed and is for relocating the memory, so I think it reorders the included files from any damage that may have happened.

The fibSquared value is located in mem[-12] (SW $3,8($2))$2=-20, then it's moved to $f0 by(mtc1 $2,$f0). In the instruction, the second par is the destination($f0). It's converted from int to float in(cvt.d.w $f0,$f0).

The sqrt executed in this part of the code:

lw  $2,%call16(sqrt)($28) \\ in this particular 
    nop
    move    $25,$2
    .reloc  1f,R_MIPS_JALR,sqrt
1:  jalr    $25
$25 at the beginning was chosen for cpload (.cpload $25) 
** printf :
lw  $2,%got($LC0)($28)
    nop
    addiu   $4,$2,%lo($LC0)
    lw  $2,%call16(printf)($28)\\ in this particular 
    nop
    move    $25,$2
    .reloc  1f,R_MIPS_JALR,printf
1:  jalr    $25
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
A.A.A
  • 1
  • 3
  • I don't think *reorders the included files from any damage that may have happened.* makes any sense. Those C `.h` files aren't included in the asm. I don't know exactly what it *does* do, but it's probably telling the assembler to emit relocation metadata that the linker might use, perhaps if you statically link it could relax the `jalr` into a simple `jal` instead of using the pointer loaded from the GOT. – Peter Cordes Jun 28 '20 at 23:04
  • @PeterCordes thank you, actually what you said makes more sense. – A.A.A Jun 29 '20 at 06:35