This MIPS assembly program converts a hexadecimal value to its...

September 15, 2025 at 03:59 AM

.eqv in_buf,0x10040020 .eqv heap, 0x10040000 #heap = 0x10040000 .eqv max, 20 .macro _done li $v0, 10 syscall .end_macro .macro _Print_str(%ptr) li $v0, 4 #print code la $a0, %ptr #address pointer syscall .end_macro .macro _GUI_out li $v0,55#print code #la $a0, hello #address (pointer) li $a1, 1 #msg type syscall .end_macro .data #setup hex2dec: .ascii "****" #reserrve word & mark val16: .word 256, 16, 1 #int[] val16={256,16,1} #hex to dec: gen output string hexu: .word 0x1050c #unpacked hexp: .ascii "15c" #packed: start of output string hexStr: .asciiz " in hex = in decimal: " #word align title: .asciiz "LAB 3: CONVERT HEX TO DECIMAL \n" prompt: .asciiz "input a new decimal number" .align 2 #align to word ovfl: .asciiz "\nError: max length exceeded!\n" error: .asciiz "\nError: canceled or empty" #extras .align 2 name2: .ascii "InputName*" .text la $a0, title _Print_str(title) _GUI_out #_done #convert hex to dec (use unrolled loop) sw $0, hex2dec #init to 0 #perform conversion jal convert #print it all #console la $a0,hexp _Print_str(hexp) jal print_int _done #GUI new dec intput jal GUI_in #stored in "in_buf" _done ##END OF MAIN## #app specific sub convert: lb $t1, hexu+2 #d2 lw $t2, val16 #v2, word mult $t1, $t2 #[hi, lo] = d2*v2 lb $t1, hexu+1 #d1 next byte lb $t2, val16+4 #v1 madd $t1, $t2 #[Hi, Lo] = d1*v1 lb $t1, hexu #d0 lb $t2, val16+8 #v0 madd $t1, $t2 #[Hi, Lo] = d0*v0 mflo $t0 #dec value sw $t0, hex2dec jr $ra #return #I/O subs print_str: li $v0, 4 la $a0, hexp syscall jr $ra print_int: li $v0, 1 move $a0, $t0 syscall jr $ra GUI_in: li $v0,54#input code la $a0, prompt #prompt address (pointer) la $a1, in_buf #input buffer pointer li $a2, max #max chars syscall jr $ra

This MIPS assembly program converts a hexadecimal value to its decimal equivalent and prints the result to both the console and a GUI (graphical user interface) using system calls. Below is an explanation of what the program does step-by-step:

Context of the Code

  1. Initialization using .eqv:

    • in_buf is a label for memory address 0x10040020. It is used as the input buffer.
    • heap is initialized to 0x10040000. This seems to be an allocated memory region.
    • max is assigned the value 20. This signifies the maximum number of characters for an input.
  2. Macros:

    • _done: This macro exits the program using syscall number 10 (li $v0, 10).
    • _Print_str: This macro is used to print a string from a specified memory address to the console.
    • _GUI_out: This macro writes output to a GUI with syscall number 55.
  3. Data Section:

    • The .data section contains predefined string and integer constants:
      • hex2dec: A 4-byte memory region to store the decimal number result.
      • val16: An array {256, 16, 1} used to perform weighted base-16 arithmetic for the conversion.
      • hexu: A hex input (unpacked) stored in memory as 0x1050c.
      • hexp: A string containing a packed version of the hex input for messages.
      • Various strings for output like title, prompt, ovfl, and error.
  4. Text Section (Code):

    • The .text section contains the program logic.

Execution Flow

  1. Print Title (LAB 3):

    • The program begins by displaying the title "LAB 3: CONVERT HEX TO DECIMAL" using the _Print_str macro.
  2. GUI Output:

    • It triggers a GUI output mechanism (using system call 55).
  3. Initialize Conversion:

    • The value at hex2dec is initialized to 0.
  4. Perform Hexadecimal to Decimal Conversion:

    • The hex value stored in hexu (0x1050c) is processed in the convert subroutine:
      • The 3 bytes of hexu (d2, d1, d0) are multiplied by their respective base-16 positional weights (val16 array: 256, 16, 1).
      • The product of each step is summed using mult and madd, with the result stored in $t0 (lower-order bits from mflo) and written to hex2dec.
  5. Display the Result on Console:

    • The program prints the result stored in hexp (the string representation of the converted number) to the console using _Print_str.
    • It then prints the decimal value using the print_int subroutine.
  6. GUI Input for a New Hex Value:

    • The program prompts the user for a new decimal number using GUI_in (system call 54) and records it in in_buf.
  7. Exit Program:

    • The program terminates using the _done macro.

Subroutines

  • convert: Converts a 3-digit hexadecimal number into its decimal equivalent using weighted arithmetic.

    • Given hexu = 0x1050c:
      • d2 = 0x10 (most significant byte): Weight 256.
      • d1 = 0x50: Weight 16.
      • d0 = 0x0c (least significant byte): Weight 1.
      • The result is stored in hex2dec.
  • print_str: Outputs a string (pointed to by $a0) to the console using syscall 4.

  • print_int: Outputs an integer value in $t0 to the console using syscall 1.

  • GUI_in: Prompts a user for new input through the GUI using syscall 54, with the input stored in in_buf.


Summary

This MIPS program converts a fixed hexadecimal value (0x1050c) into its decimal equivalent, displays it on the console and GUI, and allows the user to input new data.

  • It uses bitwise arithmetic and array-based positional weights to compute the decimal representation from the hexadecimal input.
  • The program incorporates both console and GUI-based I/O operations with system calls.
Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node