This MIPS assembly program converts a hexadecimal value to its...
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
-
Initialization using
.eqv
:in_buf
is a label for memory address0x10040020
. It is used as the input buffer.heap
is initialized to0x10040000
. This seems to be an allocated memory region.max
is assigned the value20
. This signifies the maximum number of characters for an input.
-
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.
-
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 as0x1050c
.hexp
: A string containing a packed version of the hex input for messages.- Various strings for output like
title
,prompt
,ovfl
, anderror
.
- The
-
Text Section (Code):
- The
.text
section contains the program logic.
- The
Execution Flow
-
Print Title (
LAB 3
):- The program begins by displaying the title "LAB 3: CONVERT HEX TO DECIMAL" using the
_Print_str
macro.
- The program begins by displaying the title "LAB 3: CONVERT HEX TO DECIMAL" using the
-
GUI Output:
- It triggers a GUI output mechanism (using system call
55
).
- It triggers a GUI output mechanism (using system call
-
Initialize Conversion:
- The value at
hex2dec
is initialized to 0.
- The value at
-
Perform Hexadecimal to Decimal Conversion:
- The hex value stored in
hexu
(0x1050c
) is processed in theconvert
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
andmadd
, with the result stored in$t0
(lower-order bits frommflo
) and written tohex2dec
.
- The 3 bytes of
- The hex value stored in
-
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.
- The program prints the result stored in
-
GUI Input for a New Hex Value:
- The program prompts the user for a new decimal number using
GUI_in
(system call54
) and records it inin_buf
.
- The program prompts the user for a new decimal number using
-
Exit Program:
- The program terminates using the
_done
macro.
- The program terminates using the
Subroutines
-
convert
: Converts a 3-digit hexadecimal number into its decimal equivalent using weighted arithmetic.- Given
hexu = 0x1050c
:d2 = 0x10
(most significant byte): Weight256
.d1 = 0x50
: Weight16
.d0 = 0x0c
(least significant byte): Weight1
.- The result is stored in
hex2dec
.
- Given
-
print_str
: Outputs a string (pointed to by$a0
) to the console using syscall4
. -
print_int
: Outputs an integer value in$t0
to the console using syscall1
. -
GUI_in
: Prompts a user for new input through the GUI using syscall54
, with the input stored inin_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.