In: Computer Science
Write a MIPS program to check if computer is a big-endian or little-endian system. This must be done in MIPS assembly language.
Greetings!!
Little endian:Lower byte of the data is stored at lower end of the memory.
Example,data 11223344 in a little endian machine is stored as:
2000:44
2001:33
2002:22
2003:11
Big endian:Lower byte of the data is stored at higher end of the memory
Example,data 11223344 in a little endian machine is stored as:
2000:11
2001:22
2002:33
2003:44
MIPS can use either little or big endian and is machine specific.
Code:
Logic used:First 1 word of data is stored and then read the data byte by byte along with the address and then observing the address and byte so that the endianess can be determined
.data
num: .word 0
message: .asciiz " is stored at address "
nl: .asciiz "\n"
.text
#STORE NUMBER 11223344 INTO MEMORY
li $t0,0x11223344
sw $t0,num
#GET THE ADDRESS OF THE MEMORY
la $t0,num
li $t2,0
loop:
beq $t2,4,end
#LOAD FIRST BYTE FROM THE MEMORY
lb $t1,0($t0)
#PRINT THE BYTE
move $a0,$t1
li $v0,1
syscall
#PRINT “IS STORED AT ADDRESS” MESSAGE
la $a0,message
li $v0,4
syscall
#PRINT THE BYTE ADDRESS
move $a0,$t0
li $v0,1
syscall
#PRINT NEWLINE
la $a0,nl
li $v0,4
syscall
addi $t0,$t0,1
addi $t2,$t2,1
#REPEAT
j loop
end:
#TERMINATION
li $v0,10
syscall
Screenshot:
Lower byte of the data is stored at the lower end of the memory and hence the system used little endian scheme.
[Please note that the output is in decimal and the corresponding hex values are given in red colour]
Hope this helps