• Stars
    star
    31
  • Rank 820,005 (Top 17 %)
  • Language
    Assembly
  • License
    MIT License
  • Created over 3 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

in this repository you will find codes in C and their equivalence in MIPS Assembly

In this repository you will find codes in C and their equivalence in MIPS Assembly

hello world

C
#include<stdio.h>   
int main(){  
  printf("hello world"); 
  return 0;
}   
MIPS Assembly
.data
 msg: .asciiz "hello world"
.text
 li $v0,4
 la $a0,msg
 syscall

print an integer

C
#include<stdio.h>
int main(){
int number=2;
  printf("%d",number);
  return 0;
}
MIPS Assembly
.data
 number: .word 2
.text 
 li $v0,1 
 lw $a0,number
 syscall 

print a float

C
#include<stdio.h>
int main(){
float number=2.5;
  printf("%f",number);
  return 0;
}
MIPS Assembly
.data
 number: .float 2.5
.text
 li $v0,2
 lwc1 $f12,number
 syscall 

read and print an integer

C
#include<stdio.h>
int main(){
int a;
printf("enter an integer : ");
scanf("%d",&a);
printf("your integer is : ");
printf("%d",a);
return 0;
}
MIPS Assembly
.data
 msg1: .asciiz "enter an integer : "
 msg2: .asciiz "your integer is  : "
.text
 print_msg1:li $v0,4
           la $a0,msg1
           syscall 
 read_integer:li $v0,5
              syscall 
 la $s0,($v0) #save_integer_in_s0:
 print_msg2:li $v0,4
            la $a0,msg2
            syscall              
 print_integer:li $v0,1
               la $a0,($s0)
               syscall 

addition_of_2_integers

C
#include<stdio.h>
int main()
{
  int a,b,c;
  scanf("%d%d",&a,&b);
  c=a+b;
  printf("%d",c);
  return 0;
}
MIPS Assembly
li $v0,5
syscall
move $s0,$v0
li $v0,5
syscall
move $s1,$v0
add $s2,$s1,$s0
li $v0,1
move $a0,$s2
syscall

substraction of 2 integers

C
#include<stdio.h>
int main()
{
  int a,b,c;
  scanf("%d%d",&a,&b);
  c=a-b;
  printf("%d",c);
  return 0;
}
MIPS Assembly
li $v0,5
syscall
move $s0,$v0
li $v0,5
syscall
move $s1,$v0
sub $s2,$s0,$s1
li $v0,1
move $a0,$s2
syscall 

if else statement

C
#include<stdio.h>
int main(){
int a=5,b=3,c=0;
if(a==b) c=a+b;
else c=a-b;
	printf("%d",c);
	return 0;
}
MIPS Assembly
li $t0,5       #a
li $t1,3       #b
li $t2,3       #c
beq $t0,$t1,if
j else
if:add $t2,$t0,$t1
else:sub $t2,$t0,$t1
print:
li $v0,1
move $a0,$t2
syscall 

nested if

C
#include<stdio.h>
int main(){
int a=1;
int b=4;
int c;
if(a==1)
{
	c=a;
	if(b!=3){
		c+=b;
	}	
}
printf("%d",c);
}
MIPS Assembly
li $t0,1  #a
li $t1,4  #b
beq $t0,1,if
j print
if:
  la $t2,($t0)   #c
  beq $t1,3,print 
  add $t2,$t2,$t1
print:
li $v0,1
move $a0,$t2
syscall 

switch statement

C
#include<stdio.h>
int main(){
int a;
int b=0;
scanf("%d",&a);
switch(a){
	case 0:b=-1; break;
	case 1:b=0; break;
	default :b=1; break;
}
printf("%d",b);
}
MIPS Assembly
# a====>$s0
# b====>$s1
read_a:li $v0,5
       syscall 
       la $s0,($v0)
switch:
beq $s0,0,case0
beq $s0,1,case1
j default
case0:li $s1,-1
j print
case1:li $s1,0
j print
default:li $s1,1
print:
li $v0,1
move $a0,$s1
syscall 

Power

C
#include<stdio.h>
int main(){
	int x,n;
	scanf("%d%d",&x,&n);
	int i=0;
	int p=1;
	for(i=0;i<n;i++)
	p*=x;
	printf("%d",p);
}
MIPS Assembly
read_x:li $v0,5
       syscall 
       la $s0,($v0)
read_n:li $v0,5
       syscall 
       la $s1,($v0)
li $s2,1 #p
li $t0,0
loop:
beq $t0,3,print
mult $s2,$s0
mflo  $s2
addi $t0,$t0,1
j loop
print:
li $v0,1
move $a0,$s2
syscall 

Factorial

C
#include<stdio.h>
int main(){
	int x;
	scanf("%d",&x);
	int i;
	int f=1;
	for(i=x;i>1;i--)
	f*=i;
	printf("%d",f);
}
MIPS Assembly
li $v0,5
syscall 
la $s0,($v0)
li $s2,1
la $t0,($s0)
loop:
beq $t0,1,print
mult $s2,$t0
mflo  $s2
subi $t0,$t0,1
j loop
print :
li $v0,1
la $a0,($s2)
syscall

the parity of a number using the rest of the division

C
#include<stdio.h>
int main(){
  int number;
  read:scanf("%d",&number);
  if(number%2==0){
    printf("even\n");
    goto read;
    }
  else printf("odd\n");
  goto read;
}
MIPS Assembly
.data 
 even : .asciiz "even\n"
 odd : .asciiz "odd\n"
 .text
read:li $v0,5
syscall
move $s0,$v0
li $t0,2
div $s0,$t0
mfhi $t1
beq $t1,0,iseven
li $v0,4
la $a0,odd
syscall 
j read
iseven: li $v0,4
      la $a0,even
      syscall
j read

the parity of a number using bitwise and operator

C
#include<stdio.h>
int main(){
  int number,c;
  read:scanf("%d",&number);
  if((number & 1)==0){
    printf("even\n");
    goto read;
    }
  else printf("odd\n");
  goto read;
}
MIPS Assembly
.data 
 even : .asciiz "even\n"
 odd : .asciiz "odd\n"
 .text
read:li $v0,5
syscall
move $s0,$v0
li $t0,2
andi $t0,$s0,1
beq $t0,0,iseven
li $v0,4
la $a0,odd
syscall 
j read
iseven: li $v0,4
      la $a0,even
      syscall
j read

swap 2 numbers using auxiliary variable

C
#include<stdio.h>
int main(){
  int a=3,b=7,c;
  c=a;
  a=b;
  b=c;
  print("%d\n%d",a,b);
  return 0;
}
MIPS Assembly
li $s0,3   #a
li $s1,7   #b
la $t0,($s0)
la $s0,($s1)
la $s1,($t0)
li $v0,1
la $a0,($s0)
syscall 
li $v0,1
la $a0,($s1)
syscall

swap 2 numbers using addition and subtraction

C
#include<stdio.h>
int main(){
  int a=3,b=7;
  a=a+b;
  b=a-b;
  a=a-b;
  print("%d\n%d",a,b);
  return 0;
}
MIPS Assembly
li $s0,3
li $s1,7
add $s0,$s0,$s1
sub $s1,$s0,$s1
sub $s0,$s0,$s1
li $v0,1
la $a0,($s0)
syscall 
li $v0,1
la $a0,($s1)
syscall 

swap 2 numbers using bitwise xor operator

C
#include<stdio.h>
int main(){
  int a=3,b=7;
  a=a^b;
  b=a^b;
  a=a^b;
  printf("%d\n%d",a,b);
  return 0;
}
MIPS Assembly
li $s0,3
li $s1,7
xor $s0,$s0,$s1
xor $s1,$s0,$s1
xor $s0,$s0,$s1
li $v0,1
la $a0,($s0)
syscall
li $v0,1
la $a0,($s1)
syscall   

Read and Print elements of an array

C
#include<stdio.h>
int main(){
	int arr[5];
	int i=0;
	//read
	for(i=0;i<5;i++){
	    scanf("%d",&arr[i]);
	}
	//print
	for(i=0;i<5;i++){
	    printf("%d",arr[i]);
	    printf(" ");
	}
}
MIPS Assembly
.data
 arr: .space 20    #(20=5*4(Size of word))
 space: .asciiz " "
.text 
 li $t0,0
 read:
 li $v0,5
 syscall 
 sw $v0,arr($t0)
 addi $t0,$t0,4
 blt $t0,20,read
 
 la $s0,arr
 li $t0,0
 print:
 li $v0,1
 lw $a0,($s0)
 syscall 
 addi $t0,$t0,4
 addi $s0,$s0,4
 li $v0,4
 la $a0,space
 syscall 
 blt $t0,20,print

Array's elements somme

C
#include<stdio.h>
int main(){
	int tab[10]={0,1,2,3,4,5,6,7,8,9};
	int s=0;
	int i=0;
	while(i<10){
		s+=*(tab+i); // s=s+tab[i]
		i++;
	}
	printf("%d",s);
	return 0;
}
MIPS Assembly
.data 
tab: .byte 0,1,2,3,4,5,6,7,8,9
.text 
main: 
la $t0, tab 
li $a0,0 #compteur
li $s0,0 #somme
loop:
lb $t1,($t0)
add $s0,$s0,$t1
addi $t0,$t0,1
addi $a0,$a0,1
bne $a0,10,loop

Print a random integer belongs to the interval 0,10

C
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main(){  
  srand(time(NULL));
  int r = rand()%10;
  printf("%i", r);
  return 0;
}   
MIPS Assembly

In order to get a random number we have to assign the register $v0 with the value 42. The value stored in $a1 represent the upper value of the generated random number.

.text
#get random int belongs to the interval 0,10
li $v0,42
li $a1,10
syscall 
print:
 li $v0,1
 syscall 

More Repositories

1

ziko.js

zikojs is a new javascript library with a focus on making coding effortless .
JavaScript
37
star
2

ZikoMatrix

Arduino library for creating and manipulating matrices of arbitrary size and data type. The library provides a Matrix class that can be used to create matrices, perform basic matrix operations
C++
20
star
3

awesome-processing

Processing is a flexible software sketchbook and a language for learning how to code. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology. There are tens of thousands of students, artists, designers, researchers, and hobbyists who use Processing for learning and prototyping.
Processing
19
star
4

zakarialaoui10

Config files for my GitHub profile.
JavaScript
12
star
5

ziko-kit

JavaScript
11
star
6

mapfun

mapfun is a function that applies a mapping function to an infinite number of input elements, with options to skip certain elements and selectively apply the mapping to keys and/or values of objects. The origin of this function traces back to zikojs
JavaScript
8
star
7

ziko-addons

JavaScript
8
star
8

PowerMonitor

A C++ library designed to measure the electrical characteristics of AC circuits such as voltage, current, power, reactive power, and power factor...
C++
7
star
9

Greenlab-Challenge

JavaScript
6
star
10

only-for-psychopathic-programmers

C
5
star
11

dir2tree

a user-friendly Node.js tool for creating organized json tree from a root directory .
JavaScript
5
star
12

ziko-micro

4
star
13

_ziko-lottie

Lottie player element for zikojs
JavaScript
3
star
14

Vanilla-X

3
star
15

ziko-lottie

Lottie player element for zikojs
JavaScript
2
star
16

ziko-addons-template

JavaScript
1
star
17

npm-ziko-test

JavaScript
1
star