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