Skip to main content

Converting a simple program in Python to MIPS Assembly Language using the MARS Emulator.


    We will see how the following simple python program can be written in MIPS Assembly Language using the MARS Emulator. For this, the logic of the python program should also be taken into consideration.

myString = "abcdefg"
for i in myString:
      print(i)

The output in Python shell will be:

>>> myString="abcdefg" #This is the program
>>> for i in myString:
print(i)


a
b
c
d
e
f
g

>>> 

The MIPS Assembly code to get the output of the above program will be as shown below.

_________________________________________________________________________
.text 

la  $s0, myString                      #set $s0 to the address of                                           myString


startLoop:                             #label to indicate the start                                         of the loop


lb $a0,($s0)                           #set $a0 to contain the first                                         byte in $s0 

li $v0,11                               
syscall                                #prints out the character in                                         $a0

add $s0,$s0,1                          #set $s0 to point to the next                                         character in the string


move $s1,$a0                           #move the contents in $a0 to                                         $s1 


lb $a0,newLine                         #set $a0 to label's                                                   content($a0 will contain the                                         new line character)

li $v0,11                               
syscall                                #prints a new line

move $a0,$s1                           #move the contents in $s1 to                                        $a0

    
bnez $a0,startLoop                     #jump to the start of the                                             loop if $a0 does                                                     not contain the null                                                 character

li $v0,10                              #halting the program 

syscall

.data


myString:.asciiz "abcdefg"             #the null terminated string                                           whose                                                               characters are to be printed

newLine:.asciiz "\n"                   #the new line character used                                         to print each                                                       character in a new line
_____________________________________________________________________________

Its output will be like this:

a
b
c
d
e
f
g

-- program is finished running --

This is how you can convert the above python program into MIPS Assembly Language. 

Comments

  1. what is the link to the emulator

    ReplyDelete
  2. Hello, I hope you help me to convert python to mips asap,sir.
    This is a python code.
    def checker(grid, r, c, k):
    check_row = k not in grid[r]
    check_col = k not in [grid[i][c] for i in range(9)]
    check_box = k not in [grid[i][j] for i in range(r//3*3, r//3*3+3) for j in range(c//3*3, c//3*3+3)]
    return (check_row and check_col and check_box)

    def sudoku(grid, r, c):
    if r == 9:
    return True
    elif c == 9:
    return sudoku(grid, r+1, 0)
    elif grid[r][c] != 0:
    return sudoku(grid, r, c+1)
    else:
    for k in range(1, 10):
    if checker(grid, r, c, k) == True:
    grid[r][c] = k
    if sudoku(grid, r, c+1) == True:
    return True
    grid[r][c] = 0
    return False

    #take input
    board = []
    i = 0
    while i < 9:
    given = list(input())
    given = [int(x) for x in given]
    board.insert(i,given)
    i = i+1
    #main+print
    sudoku(board, 0, 0)
    for x in board:
    print(*x, sep='')





    ReplyDelete

Post a Comment

Popular posts from this blog

How to import the Public Certificate of one WSO2 product to the trust store of another?

To demonstrate this point, I will use the 2 products WSO2 API Manager 2.1.0 (referred as APIM from here onwards) and WSO2 Enterprise Integrator 6.1.1 (referred as EI from here onwards). When using EI as the Business Process Server during configuration of Workflows in APIM, one step to perform is to import the public certificate of EI to the truststore of APIM [1]. So now let's see how this can be done. Step 1: Go to <EI_HOME>/repository/resources/security/ folder and execute the following keytool command. This command is used to export the public certificate of EI as a certificate file called wso2carbon.cer. Since the default keystore in EI is wso2carbon.jks, we have specified it as the keystore and the default alias is wso2carbon. Provide wso2carbon as the keystore password when prompted as it is the default password. After executing the above command from within the security folder in EI, you will see that a file with the name of wso2carbon.cer is created...

How to connect my database instance with elastic beanstalk instance in AWS?

If you have deployed your web application in Elastic Beanstalk in AWS and now you need to connect a database to this instance, and your database is actually residing in a different instance, how can you actually connect them? It's easy with Elastic Beanstalk. I will explain an example scenario that I used for connecting my elastic beanstalk web application with another instance containing my MongoDB database. By looking at this, you can customize as per your need. Don't worry. This is easy. :) The only things you need here are the details about the 1. Database name that you need to connect to. Ex:- "myDB" 2. Port at which the database instance is listening. EX:- In the case of MongoDB, the listening port is 27017 3. Host name of your database instance. EX:- Like localhost, in this case, it will be the Public DNS of your database instance 4. The password of your database if exists. First these details need to be set as environment variables in Elastic Be...