Please help me to convert this C code to assembly language of MIPS?

Thread Starter

Ryan$

Joined Dec 14, 2018
178
Hi guys, may please help me to convert this code to assembly language of MIPS?

int binom(n, k)
{
if (k == 0 || n == k)
return 1;
return binom(n-1, k-1) + binom(n-1, k)
}

I know that compiler can convert c language to assembly language but it converts in assembly of i7 and not of MIPS!


thanks beforehand !
 

nsaspook

Joined Aug 27, 2009
12,998
Easy, just write code for the PIC32 using xc32. It's a MIPS machine.
C:
70:                  int binom(int n, int k)
71:                  {
9D0068C0  27BDFFE0   ADDIU SP, SP, -32
9D0068C4  AFBF001C   SW RA, 28(SP)
9D0068C8  AFB20018   SW S2, 24(SP)
9D0068CC  AFB10014   SW S1, 20(SP)
9D0068D0  AFB00010   SW S0, 16(SP)
9D0068D4  00A08021   ADDU S0, A1, ZERO
72:                      if (k == 0 || n == k)
9D0068D8  10A0000B   BEQ A1, ZERO, .LVL4
9D0068DC  24020001   ADDIU V0, ZERO, 1
9D0068E0  10850009   BEQ A0, A1, .LVL4
9D0068E4  2491FFFF   ADDIU S1, A0, -1
73:                          return 1;
74:                      return binom(n - 1, k - 1) + binom(n - 1, k);
9D0068E8  02202021   ADDU A0, S1, ZERO
9D0068EC  0F401A30   JAL binom
9D0068F0  24A5FFFF   ADDIU A1, A1, -1
9D0068F4  00409021   ADDU S2, V0, ZERO
9D0068F8  02202021   ADDU A0, S1, ZERO
9D0068FC  0F401A30   JAL binom
9D006900  02002821   ADDU A1, S0, ZERO
9D006904  02421021   ADDU V0, S2, V0
75:                  }
9D006908  8FBF001C   LW RA, 28(SP)
9D00690C  8FB20018   LW S2, 24(SP)
9D006910  8FB10014   LW S1, 20(SP)
9D006914  8FB00010   LW S0, 16(SP)
9D006918  03E00008   JR RA
9D00691C  27BD0020   ADDIU SP, SP, 32
76:
 

Thread Starter

Ryan$

Joined Dec 14, 2018
178
Easy, just write code for the PIC32 using xc32. It's a MIPS machine.
C:
70:                  int binom(int n, int k)
71:                  {
9D0068C0  27BDFFE0   ADDIU SP, SP, -32
9D0068C4  AFBF001C   SW RA, 28(SP)
9D0068C8  AFB20018   SW S2, 24(SP)
9D0068CC  AFB10014   SW S1, 20(SP)
9D0068D0  AFB00010   SW S0, 16(SP)
9D0068D4  00A08021   ADDU S0, A1, ZERO
72:                      if (k == 0 || n == k)
9D0068D8  10A0000B   BEQ A1, ZERO, .LVL4
9D0068DC  24020001   ADDIU V0, ZERO, 1
9D0068E0  10850009   BEQ A0, A1, .LVL4
9D0068E4  2491FFFF   ADDIU S1, A0, -1
73:                          return 1;
74:                      return binom(n - 1, k - 1) + binom(n - 1, k);
9D0068E8  02202021   ADDU A0, S1, ZERO
9D0068EC  0F401A30   JAL binom
9D0068F0  24A5FFFF   ADDIU A1, A1, -1
9D0068F4  00409021   ADDU S2, V0, ZERO
9D0068F8  02202021   ADDU A0, S1, ZERO
9D0068FC  0F401A30   JAL binom
9D006900  02002821   ADDU A1, S0, ZERO
9D006904  02421021   ADDU V0, S2, V0
75:                  }
9D006908  8FBF001C   LW RA, 28(SP)
9D00690C  8FB20018   LW S2, 24(SP)
9D006910  8FB10014   LW S1, 20(SP)
9D006914  8FB00010   LW S0, 16(SP)
9D006918  03E00008   JR RA
9D00691C  27BD0020   ADDIU SP, SP, 32
76:
Thanks alot ! I will make sure to download the xc32 because it sounds it helps alot !

may you submit the code for binom(int n, int k) but not in a recursive way?
by the way it's not a homework * it's just to be aware of how things go beyond the compiler when my code is in a recursive or not recursive.

also sorry for not mention that in my thread , but the pattern of assembly code that I'm going to input it to my C programming code(yup entering my assembly code to C code) is like this pattern for instance(32 bits):

limm $a0, $zero, $zero, 1024 # Load the start address of the array

so what u've written above will not help me :(


thanks alot!
 
Last edited:

MrSoftware

Joined Oct 29, 2013
2,186
Ya learn something new everyday :)
Indeed! When he said MIPS, I thought maybe he had dug up an old SGI machine or something, I did not realize lots of those little embedded processors were MIPS!

It's not clear why what he posted won't work for you? For assembly, you need to be sure the instructions you are using are supported by the assembler for your platform. For example, the "limm" instruction might not be supported by MIPS, so you need to get a reference sheet for the instructions supported by your MIPS assembler. In a higher level language like C, you can use the same operations across platforms and the compiler hides the implementation from you. In assembly it's up to you to know the actual instructions supported by the assembler, and that list of instructions will be different for different processor architectures. For example, on x86 the FBLD instruction will load a binary coded decimal value. On the MIPS platform, no such instruction exists.
 
Last edited:

nsaspook

Joined Aug 27, 2009
12,998
Indeed! When he said MIPS, I thought maybe he had dug up an old SGI machine or something, I did not realize lots of those little embedded processors were MIPS!
The core model is the MIPS32R2 using the MIPS coprocessor and vector interfaces for typical micro-controller peripheral modules. You don't need the Microchip compiler to code the PIC32 cpu core but you will need the correct header files to access the Microchip specific memory mapped peripherals and interrupts to program I/O.

https://www.mips.com/develop/tools/codescape-mips-sdk/

An example of using this SDK with the pic32mzef riot-os port is here: https://github.com/nsaspook/RIOT/tree/PIC32MZEF/boards/pic32-cpicmzef
https://raw.githubusercontent.com/nsaspook/RIOT/PIC32MZEF/boards/pic32-cpicmzef/Makefile.features
C:
# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_adc
FEATURES_PROVIDED += periph_dac
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart

# The board MPU family (used for grouping by the CI system)
FEATURES_MCU_GROUP = mips32r2

-include $(RIOTCPU)/mips_pic32mzef/Makefile.features
 
Last edited:

Thread Starter

Ryan$

Joined Dec 14, 2018
178
Easy, just write code for the PIC32 using xc32. It's a MIPS machine.
C:
70:                  int binom(int n, int k)
71:                  {
9D0068C0  27BDFFE0   ADDIU SP, SP, -32
9D0068C4  AFBF001C   SW RA, 28(SP)
9D0068C8  AFB20018   SW S2, 24(SP)
9D0068CC  AFB10014   SW S1, 20(SP)
9D0068D0  AFB00010   SW S0, 16(SP)
9D0068D4  00A08021   ADDU S0, A1, ZERO
72:                      if (k == 0 || n == k)
9D0068D8  10A0000B   BEQ A1, ZERO, .LVL4
9D0068DC  24020001   ADDIU V0, ZERO, 1
9D0068E0  10850009   BEQ A0, A1, .LVL4
9D0068E4  2491FFFF   ADDIU S1, A0, -1
73:                          return 1;
74:                      return binom(n - 1, k - 1) + binom(n - 1, k);
9D0068E8  02202021   ADDU A0, S1, ZERO
9D0068EC  0F401A30   JAL binom
9D0068F0  24A5FFFF   ADDIU A1, A1, -1
9D0068F4  00409021   ADDU S2, V0, ZERO
9D0068F8  02202021   ADDU A0, S1, ZERO
9D0068FC  0F401A30   JAL binom
9D006900  02002821   ADDU A1, S0, ZERO
9D006904  02421021   ADDU V0, S2, V0
75:                  }
9D006908  8FBF001C   LW RA, 28(SP)
9D00690C  8FB20018   LW S2, 24(SP)
9D006910  8FB10014   LW S1, 20(SP)
9D006914  8FB00010   LW S0, 16(SP)
9D006918  03E00008   JR RA
9D00691C  27BD0020   ADDIU SP, SP, 32
76:
could you please show me the assembly of the same function but not in recursive, in an iterative way?
 

Thread Starter

Ryan$

Joined Dec 14, 2018
178
Easy, just write code for the PIC32 using xc32. It's a MIPS machine.
C:
70:                  int binom(int n, int k)
71:                  {
9D0068C0  27BDFFE0   ADDIU SP, SP, -32
9D0068C4  AFBF001C   SW RA, 28(SP)
9D0068C8  AFB20018   SW S2, 24(SP)
9D0068CC  AFB10014   SW S1, 20(SP)
9D0068D0  AFB00010   SW S0, 16(SP)
9D0068D4  00A08021   ADDU S0, A1, ZERO
72:                      if (k == 0 || n == k)
9D0068D8  10A0000B   BEQ A1, ZERO, .LVL4
9D0068DC  24020001   ADDIU V0, ZERO, 1
9D0068E0  10850009   BEQ A0, A1, .LVL4
9D0068E4  2491FFFF   ADDIU S1, A0, -1
73:                          return 1;
74:                      return binom(n - 1, k - 1) + binom(n - 1, k);
9D0068E8  02202021   ADDU A0, S1, ZERO
9D0068EC  0F401A30   JAL binom
9D0068F0  24A5FFFF   ADDIU A1, A1, -1
9D0068F4  00409021   ADDU S2, V0, ZERO
9D0068F8  02202021   ADDU A0, S1, ZERO
9D0068FC  0F401A30   JAL binom
9D006900  02002821   ADDU A1, S0, ZERO
9D006904  02421021   ADDU V0, S2, V0
75:                  }
9D006908  8FBF001C   LW RA, 28(SP)
9D00690C  8FB20018   LW S2, 24(SP)
9D006910  8FB10014   LW S1, 20(SP)
9D006914  8FB00010   LW S0, 16(SP)
9D006918  03E00008   JR RA
9D00691C  27BD0020   ADDIU SP, SP, 32
76:
and I'm not understanding what's ".LVL4" ?! label to what?
 

Thread Starter

Ryan$

Joined Dec 14, 2018
178
You've been provided information to find the answers. If it's important, take the time to install the compiler software.
Well that's correct I promised and here I've done my code of binom in assembly language of SIMP(it's MIPS but alil updated) .. :
(the format pattern that I'm working with is, 32:0 bits which 31:28 is opcode, 27:24 is rd, 23:20 is rs, 19:16 is rt, 15:12 rm, 11:0 immedaiate )

add $sp, $zero, $zero, $zero, -32
sw $ra, $zero, $sp, $zero, 28
sw $s2, $zero, $sp, $zero, 24
sw $s1, $zero, $sp, $zero, 20
sw $so, $zero, $sp, $zero, 16
add $sp, $a1, $zer0, $zero, $zero
branch $a1, $zero, $zero, 0, .LVL4
add $v0, $zero, $zero, $zero, 1
branch $a0, $a1, $zero, 0, .LVL4
add $s1, $zero, $zero, $a0, -1
add $a0, $zero, $zero, $s1, 0
jal $zero, $zero, $zero, $zero, binom
add $a1, $zero, $zero, $a1, -1
add $s2, $zero, $zero, $v0, 0
add $a0, $zero, $zero, $s1, 0
jal binom
add $a1, $zero, $zero, $s0, 0
add $v0, $zero, $zero, $s2, $v0
lw $ra, $zero, $sp, $zero, 28
lw $s2, $zero, $sp, $zero, 24
lw $s1, $zero, $sp, $zero, 20
lw $s0, $zero, $sp, $zero, 16
jr $ra, $zero, $sp, $zero, 0
add $sp, $sp, $zero, $zero, 32

it doesn't work correctly, and frankly I don't know why, may you please help me to re-correct my code? I appreciate your cooperative in advance.
 
Top