def hexBin(hexvalue): return format(int(hexvalue, 16), '032b') def printregisters(reg):…
- User
def hexBin(hex_value): return format(int(hex_value, 16), '032b')
def print_registers(reg): print("Registers:") for i in range(0, 32, 4): # Print 4 registers per line print( f"{i:2}: {reg[f'{i}']:10} | {i+1:2}: {reg[f'{i+1}']:10} | {i+2:2}: {reg[f'{i+2}']:10} | {i+3:2}: {reg[f'{i+3}']:10}" ) print()
def print_memory(memory): print("Memory:") non_zero_addresses = [ address for address, value in memory.items() if value != 0 ] if not non_zero_addresses: print("\nAll memory values are 0.\n") return
min_address = min(non_zero_addresses) max_address = max(non_zero_addresses) start_address = min_address - (min_address % 4) while max_address - start_address < 32 * 4: max_address += 4 for address in range(start_address, max_address + 1, 16): for offset in range(0, 16, 4): current_address = address + offset value = memory.get(current_address, 0) print(f"M[{current_address:5}]: {value:10}", end=" | ") print() print()def toAsm(mc, asm): op = { "001000": "addi", "000100": "beq", "000101": "bne", "000010": "j", "100011": "lw", "101011": "sw", }
functs = { "100100": "and", '000000': 'sll', '000010': 'srl', '100010': 'sub', '101010': 'slt', "100000": 'add', "111111": "calc", } opcode = mc[:6] if opcode == '000000': # R-types rs = mc[6:11] rt = mc[11:16] rd = mc[16:21] shamt = mc[21:26] funct = mc[26:] if funct in functs: if funct == '000000' or funct == '000010': asm.append( f"{functs[funct]} ${int(rd, 2)}, ${int(rt, 2)}, {int(shamt, 2)}" ) elif funct == '111111': asm.append(f"{functs[funct]} ${int(rd, 2)}, ${int(rs, 2)}") else: asm.append(f"{functs[funct]} ${int(rd, 2)}, ${int(rs, 2)}, ${int(rt, 2)}") elif opcode in op: # I-types rs = mc[6:11] rt = mc[11:16] imm0 = mc[16:] imm = int(imm0, 2) if imm0[0] == '1': # Sign extension for negative numbers imm -= 1 << len(imm0) if opcode == '100011' or opcode == '101011': asm.append(f"{op[opcode]} ${int(rt, 2)}, {imm}(${int(rs, 2)})") elif opcode == '000010': asm.append(f"{op[opcode]} {imm}") else: asm.append(f"{op[opcode]} ${int(rt, 2)}, ${int(rs, 2)}, {imm}")def sim(inD, reg, memory, binList): PC = 0 counts = { 'Total': 0, 'ALU': 0, 'Jump': 0, 'Branch': 0, 'Memory': 0, 'Other': 0, 'Special': 0 }
- Assistant
- User
rewrite complete code
- Assistant
- User
PS C:\Users\91798\Mips_simulator> & C:/Users/91798/anaconda3/python.exe c:/Users/91798/Mips_simulator/test2.py Machine Code Instructions: 00000001010010110110000000101010 00000001011010100110100000101010 only processsing two commands addi 0, 15
addi 0, 20
addi 8, 10
addi 9, -5 slt 10, 13, 10 addi 12, 3 addi 13, 2 addi 14, 4 addi 15, 5 - Assistant
- User
reewrite the code and print the binary as this format like string first and binary code related to it aftwerwards
- Assistant
- User
implement compelete code
- Assistant
- User
addi 0, 15
addi 0, 20
addi 8, 10
addi 9, -5
slt 10, 13, 10 addi 12, 3 addi 13, 2 addi 14, 4 addi 15, 5 it might not be reading addi because of the intergervalue as second arguiment - Assistant
- User
write loop test code
- Assistant
- User
donot use nop
- Assistant
- User
only write .asm code
- Assistant
Sure, here is the MIPS assembly code for a simple decrementing loop that does not use the
nopinstruction. This loop initializes a counter in a register and decrements it while incrementing another register to count the number of iterations:# A simple decrementing loop addi $1, $0, 10 # Initialize counter in $1 to 10 addi $2, $0, 0 # Initialize loop counter in $2 to 0 loop: beq $1, $0, end # If $1 (counter) is 0, exit the loop addi $2, $2, 1 # Increment loop counter $2 addi $1, $1, -1 # Decrement counter $1 j loop # Jump back to the beginning of the loop end: # End of the loopDescription:
addi $1, $0, 10: Sets register$1as a counter starting at 10.addi $2, $0, 0: Initializes the loop counter to 0.
- User
- Assistant
- User
- Assistant
- User
rewrite complete implemented upadated code
- Assistant
- User
Executing Instruction: Assembly Code: j 2 Machine Code: 00001000000000000000000000000010 PC before execution: 20 PC after execution: 20 Registers: 1: 9 | 3: 0 5: 0 | 7: 0 9: 0 | 11: 0 13: 0 | 15: 0 17: 0 | 19: 0 21: 0 | 23: 0 25: 0 | 27: 0 29: 0 | 31: 0
- Assistant
- User
A simple decrementing loop
addi 0, 10 # Initialize counter in 2, 0, 0 # Initialize loop counter in 2 to 0
loop: beq 0, end # If 2, 2 addi 1, -1 # Decrement counter $1 j loop # Jump back to the beginning of the loop
- Assistant
- User
- Assistant
- User
PS C:\Users\91798\Mips_simulator> & C:/Users/91798/anaconda3/python.exe c:/Users/91798/Mips_simulator/test2.py Assembly to Machine Code Conversion: Enter 'n' for single instruction mode, 'a' for automatic mode: n
Execution Completed. Instructions Executed: 0 ALU Instructions: 0 Jump Instructions: 0 Branch Instructions: 0 Memory Instructions: 0 Special Instructions: 0 Other Instructions: 0 Registers: 1: 0 | 3: 0 5: 0 | 7: 0 9: 0 | 11: 0 13: 0 | 15: 0 17: 0 | 19: 0 21: 0 | 23: 0 25: 0 | 27: 0 29: 0 | 31: 0
- Assistant