This code should be Runnable on MARS (MIPS Assembler and Runtime Simulator) IDE
Convert the following c code into mips
#include <stdio.h>
#include <string.h>
char cipherText[200] = "anything";
int countNumberOfCharInCipher(char*);
int countNumberOfCharInCipher(char* cText)
{
return strlen(cText);
}
int countSpaces(int numberOfChar, char input[])
{
int spaceCounter =0;
for(int i=0; i < numberOfChar; i++)
{
if(input[i] == 32)
spaceCounter++;
}
return spaceCounter;
}
void decrypt(int numberOfChar, int key, char * cipherText, char * plainText)
{
int j = 0;
int i =0;
int k = 0;
int reverseOrderedKey[4];
int n =4;
int c =0;
int d =0;
int testKey[4] = {1,2,3,4};
int orderedKey[4] = {0,0,0,0};
while(key)
{
// printf("key : %d\n", key % 10);
reverseOrderedKey[k] = key %10;
key /= 10;
k++;
}
for (c = n - 1, d = 0; c >= 0; c--, d++)
orderedKey[d] = reverseOrderedKey[c];
for ( j=0; j <= 4; j++)
{
for( i=0; i<numberOfChar; i++)
{{
plainText[i] = cipherText[i] - orderedKey[j];
}
j += 1;
if (j %4 == 0) j = 0;
}
if (i == strlen(cipherText)) break;
}
}
int main()
{
char decrypttStr[1000] = "main text ";
int numChar = countNumberOfCharInCipher(decrypttStr);
int bestSpaces =0;
int bestKey = 0;
char plainText[1000];
for (int key = 1000; key < 10000; key++)
{
decrypt(numChar, key, decrypttStr, plainText);
int numSpaces = countSpaces(numChar, plainText);
if (numSpaces > bestSpaces)
{
bestSpaces = numSpaces ;
bestKey = key ;
printf("numSpaces : %d, bestKey : %d, plainText : %s",numSpaces, bestKey, plainText);
printf("key : %d, numSpaces : %d\n", key, numSpaces);
}
}
}
Here is the equivalent MIPS code:
.file 1 ""
.section .mdebug.abi32
.previous
.nan legacy
.module fp=32
.module nooddspreg
.abicalls
.text
.align 2
.globl countNumberOfCharInCipher
.set nomips16
.set nomicromips
.ent countNumberOfCharInCipher
.type countNumberOfCharInCipher,
@function
countNumberOfCharInCipher:
.frame $sp,0,$31
# vars= 0, regs= 0/0, args= 0, gp= 0
.mask 0x00000000,0
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
lw $25,%call16(strlen)($28)
nop
.reloc 1f,R_MIPS_JALR,strlen
1: jr $25
nop
.set macro
.set reorder
.end countNumberOfCharInCipher
.size countNumberOfCharInCipher,
.-countNumberOfCharInCipher
.align 2
.globl countSpaces
.set nomips16
.set nomicromips
.ent countSpaces
.type countSpaces, @function
countSpaces:
.frame $sp,0,$31
# vars= 0, regs= 0/0, args= 0, gp= 0
.mask 0x00000000,0
.fmask 0x00000000,0
.set noreorder
.set nomacro
move $6,$5
move $2,$0
$L3:
subu $3,$6,$5
slt $3,$3,$4
beq $3,$0,$L8
movz $31,$31,$0
nop
lb $3,0($6)
addiu $6,$6,1
xori $3,$3,0x20
sltu $3,$3,1
b $L3
addu $2,$2,$3
$L8:
j $31
nop
.set macro
.set reorder
.end countSpaces
.size countSpaces, .-countSpaces
.align 2
.globl decrypt
.set nomips16
.set nomicromips
.ent decrypt
.type decrypt, @function
decrypt:
.frame $sp,80,$31
# vars= 32, regs= 6/0, args= 16, gp= 8
.mask 0x801f0000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-80
addiu $2,$sp,40
li $3,10
# 0xa
sw $20,72($sp)
sw $18,64($sp)
movz $31,$31,$0
sw $17,60($sp)
.cprestore 16
sw $31,76($sp)
sw $19,68($sp)
sw $16,56($sp)
move $18,$4
move $17,$6
move $20,$7
$L10:
beq $5,$0,$L19
addiu $2,$2,4
bne $3,$0,1f
div $0,$5,$3
break 7
1:
mfhi $4
mflo $5
b $L10
sw $4,-4($2)
$L19:
lw $2,52($sp)
move $16,$0
sw $2,24($sp)
lw $2,48($sp)
addiu $19,$sp,24
sw $2,28($sp)
lw $2,44($sp)
nop
sw $2,32($sp)
lw $2,40($sp)
nop
sw $2,36($sp)
move $3,$20
$L21:
move $2,$17
$L17:
subu $4,$2,$17
slt $4,$4,$18
beq $4,$0,$L20
sll $4,$16,2
addu $4,$19,$4
lw $5,0($4)
lbu $4,0($2)
addiu $16,$16,1
subu $4,$4,$5
sb $4,0($3)
andi $4,$16,0x3
bne $4,$0,$L13
nop
move $16,$0
$L13:
addiu $2,$2,1
b $L17
addiu $3,$3,1
$L20:
lw $25,%call16(strlen)($28)
nop
.reloc 1f,R_MIPS_JALR,strlen
1: jalr $25
move $4,$17
lw $28,16($sp)
bgez $18,$L16
move $3,$18
move $3,$0
$L16:
beq $2,$3,$L9
addiu $16,$16,1
slt $2,$16,5
bne $2,$0,$L21
move $3,$20
$L9:
lw $31,76($sp)
lw $20,72($sp)
lw $19,68($sp)
lw $18,64($sp)
lw $17,60($sp)
lw $16,56($sp)
j $31
addiu $sp,$sp,80
.set macro
.set reorder
.end decrypt
.size decrypt, .-decrypt
.section
.rodata.str1.4,"aMS",@progbits,1
.align 2
$LC2:
.ascii "numSpaces : %d, bestKey : %d,
plainText : %s\000"
.align 2
$LC3:
.ascii "key : %d, numSpaces :
%d\012\000"
.rdata
.align 2
$LC1:
.ascii "main text \000"
.space 989
.section
.text.startup,"ax",@progbits
.align 2
.globl main
.set nomips16
.set nomicromips
.ent main
.type main, @function
main:
.frame $sp,2072,$31
# vars= 2000, regs= 9/0, args= 24, gp= 8
.mask 0x80ff0000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-2072
lw $5,%got($LC1)($28)
lw $25,%call16(memcpy)($28)
sw $18,2044($sp)
addiu $18,$sp,1032
movz $31,$31,$0
sw $31,2068($sp)
.cprestore 24
sw $23,2064($sp)
sw $22,2060($sp)
sw $21,2056($sp)
sw $20,2052($sp)
sw $19,2048($sp)
sw $16,2036($sp)
li $6,11
# 0xb
addiu $5,$5,%lo($LC1)
sw $17,2040($sp)
.reloc 1f,R_MIPS_JALR,memcpy
1: jalr $25
move $4,$18
lw $28,24($sp)
li $6,989
# 0x3dd
lw $25,%call16(memset)($28)
move $5,$0
.reloc 1f,R_MIPS_JALR,memset
1: jalr $25
addiu $4,$sp,1043
lw $28,24($sp)
move $4,$18
lw $25,%call16(strlen)($28)
nop
.reloc 1f,R_MIPS_JALR,strlen
1: jalr $25
li $16,1000
# 0x3e8
lw $28,24($sp)
move $23,$2
lw $20,%got($LC2)($28)
lw $21,%got($LC3)($28)
move $22,$0
addiu $19,$sp,32
addiu $20,$20,%lo($LC2)
addiu $21,$21,%lo($LC3)
$L24:
lw $25,%got(decrypt)($28)
move $7,$19
move $6,$18
move $5,$16
.reloc 1f,R_MIPS_JALR,decrypt
1: jalr $25
move $4,$23
lw $28,24($sp)
move $5,$19
lw $25,%got(countSpaces)($28)
nop
.reloc 1f,R_MIPS_JALR,countSpaces
1: jalr $25
move $4,$23
move $17,$2
slt $2,$22,$2
lw $28,24($sp)
beq $2,$0,$L23
move $7,$16
lw $25,%call16(__printf_chk)($28)
move $6,$17
move $5,$20
li $4,1
# 0x1
.reloc 1f,R_MIPS_JALR,__printf_chk
1: jalr $25
sw $19,16($sp)
lw $28,24($sp)
move $7,$17
lw $25,%call16(__printf_chk)($28)
move $6,$16
move $5,$21
.reloc 1f,R_MIPS_JALR,__printf_chk
1: jalr $25
li $4,1
# 0x1
lw $28,24($sp)
move $22,$17
$L23:
addiu $16,$16,1
li $2,10000
# 0x2710
bne $16,$2,$L24
move $2,$0
lw $31,2068($sp)
lw $23,2064($sp)
lw $22,2060($sp)
lw $21,2056($sp)
lw $20,2052($sp)
lw $19,2048($sp)
lw $18,2044($sp)
lw $17,2040($sp)
lw $16,2036($sp)
j $31
addiu $sp,$sp,2072
.set macro
.set reorder
.end main
.size main, .-main
.globl cipherText
.data
.align 2
.type cipherText, @object
.size cipherText, 200
cipherText:
.ascii "anything\000"
.space 191
.ident "GCC: (Ubuntu
7.4.0-1ubuntu1~18.04.1) 7.4.0 20180726"
If you find it difficult to understand the code, just leave a question with code line.
Thanks for using homeworklib!
This code should be Runnable on MARS (MIPS Assembler and Runtime Simulator) IDE Convert the following...
CONVERT THE FOLLOWING FUNCTION IN MARS SIMULATOR (MIPS) This FUNCTION should be Runnable on MARS (MIPS Assembler and Runtime Simulator) IDE C FUNCTION WHICH NEEDS TO BE CONVERTED (ONLY USE MARS) void decrypt(int numberOfChar, int key, char * cipherText, char * plainText) { int j = 0; int i =0; int k = 0; int reverseOrderedKey[4]; int n =4; int c =0; int d =0; int orderedKey[4] = {0,0,0,0}; while(key) { reverseOrderedKey[k] = key %10; key /= 10; k++; }...
Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...
Question: Please Provide Comments on each Line of code explaining what the C Function is doing throughout the code. // Function used for substitution encryption void SubEncrypt(char *message, char *encryptKey) { int iteration = 0; printf("Enter Aphabet Encryption Key: \n"); scanf("%s", encryptKey); for (iteration = 0; iteration < strlen(message); iteration++) { char letter = message[iteration]; if (letter >= 'A' && letter <= 'Z') { letter = encryptKey[letter - 'A']; } message[iteration] = letter; } printf("CipherText message: %s\n", message); } //_________________________________________________________________________________________________________________________________________________...
Convert the below C code to basic MIPS. The result of this code should be a^b of the two numbers that you input. For example if you input the numbers 3 and 5 you should get a result of 3^5=6 #include int main(void) { printf("Insert two numbers\n"); int a,b,c; scanf("%d",&a); scanf("%d",&b); c=a^b; printf("%d^%d=%d\n",a,b,c); return 0; }
Write helpful comments for the
following code: use Vigenere cipher tech to encrypt and decrypt
message
The code:
#include<stdio.h>
#include <stdlib.h>
char arr[26][26];
char message[22], key[22], emessage[22], retMessage[22];
int findRow(char);
int findColumn(char);
int findDecRow(char, int);
int main()
{
int i = 0, j, k, r, c;
k = 96;
for (i = 0; i<26; i++)
{
k++;
for (j = 0; j<26; j++)
{
arr[i][j] = k++;
if (k == 123)
k = 97;
}
}
printf("\nEnter message\n");
fgets(message, 22,...
How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...
C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...
C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...
Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...
Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){ char str[1000], ch; int i, frequency = 0; clock_t t; struct timespec ts, ts2; printf("Enter a string: "); gets(str); int len = strlen(str); printf("Enter a character...