Question: my code but the output is not correct and im not sure why, also see pic for more ################################################################################ # MIPS assembly language translation of
my code but the output is not correct and im not sure why, also see pic for more
################################################################################
# MIPS assembly language translation of a given C++ program that, except for the
# main function, involves "trivial" functions each of which:
# - is a leaf function
# - does not require local storage (on the stack)
# NOTES:
# - "does not require local storage" means each (leaf) function
# -- does not need memory on the stack for local variables (including arrays)
# -- WILL NOT use any callee-saved registers ($s0 through $s7)
# - meant as an exercise for familiarizing w/ the
# -- basics of MIPS' function-call mechanism
# -- how-to's of pass-by-value & pass-by-address when doing functions in MIPS
# - does NOT adhere to yet-to-be-studied function-call convention (which is
# needed when doing functions in general, not just "trivial" functions)
# - main (being the only non-"trivial" function & an unavoidable one) will in
# fact violate the yet-to-be-studied function-call convention
# -- due to this, each of the functions that main calls MUST TAKE ANOMALOUS
# CARE not to "clobber" the contents of registers that main uses & expects
# to be preserved across calls
# -- experiencing the pains and appreciating the undesirability of having to
# deal with the ANOMALOUS SITUATION (due to the non-observance of any
# function-call convention that governs caller-callee relationship) should
# help in understanding why some function-call convention must be defined
# and observed
################################################################################
# Algorithm used:
# Given C++ program (Assign03P2.cpp)
################################################################################
# Sample test run:
##################
#
# vals to do? 4
# enter an int: 1
# enter an int: 2
# enter an int: 3
# enter an int: 4
# initial:
# 1 2 3 4
# flipped:
# 4 3 2 1
# do more? y
# vals to do? 0
# 0 is bad, make it 1
# enter an int: 5
# initial:
# 5
# flipped:
# 5
# do more? y
# vals to do? 8
# 8 is bad, make it 7
# enter an int: 7
# enter an int: 6
# enter an int: 5
# enter an int: 4
# enter an int: 3
# enter an int: 2
# enter an int: 1
# initial:
# 7 6 5 4 3 2 1
# flipped:
# 1 2 3 4 5 6 7
# do more? n
# -- program is finished running --
################################################################################
# int GetOneIntByVal(const char vtdPrompt[]);
# void GetOneIntByAddr(int* intVarToPutInPtr,const char entIntPrompt[]);
# void GetOneCharByAddr(char* charVarToPutInPtr, const char prompt[]);
# void ValidateInt(int* givenIntPtr, int minInt, int maxInt, const char msg[]);
# void SwapTwoInts(int* intPtr1, int* intPtr2);
# void ShowIntArray(const int array[], int size, const char label[]);
#
#int main()
#{
.text
.globl main
main:
# int intArr[7];
# int valsToDo;
# char reply;
# char vtdPrompt[] = "vals to do? ";
# char entIntPrompt[] = "enter an int: ";
# char adjMsg[] = " is bad, make it ";
# char initLab[] = "initial: ";
# char flipLab[] = "flipped: ";
# char dmPrompt[] = "do more? ";
# int i, j;
#################
# Register Usage:
#################
# $t0: register holder for a value
# $t1: i
# $t2: j
#################
addiu $sp, $sp, -109
j StrInitCode # "atypical" use of function to reduce clutter
endStrInit:
# do
# {
begWBodyM1:
# valsToDo = GetOneIntByVal(vtdPrompt);
li $a0, ' '
li $v0, 11
syscall # ' ' to offset effects of syscall #12 drawback
move $a0, $sp
jal GetOneIntByVal
sw $v0, 77($sp)
# ValidateInt(&valsToDo, 1, 7, adjMsg);
####################(4)####################
# pass &valsToDo
addi $a0, $sp, 77
# li for 1
li $a1, 1
# li for 7
li $a2, 7
# adjMsg in register
addi $a3, $sp, 28
###########################################
jal ValidateInt
# for (i = valsToDo; i > 0; --i)
lw $t1, 77($sp)
j FTestM1
begFBodyM1:
# if (i % 2) // i is odd
andi $t0, $t1, 0x00000001
beqz $t0, ElseI1
# intArr[valsToDo - i] = GetOneIntByVal(entIntPrompt);
####################(8)####################
addi $a0, $sp, 13
jal GetOneIntByVal
lw $a1, 77($sp)
addi $t0, $sp, 81
sub $a3, $a1, $t1 # $a3 has valsToDo - i
sll $a3, $a3, 2 # $a3 has (valsToDo - i)*4
add $a3, $a3, $t0 # $a3 has &intArr[valsToDo - i]
sw $v0, 0($a3) # $a0 has intArr[valsToDo - 1]
###########################################
j endI1
# else // i is even
ElseI1:
# GetOneIntByAddr(intArr + valsToDo - i, entIntPrompt);
####################(7)####################
lw $a0, 77($sp) #valsToDo
sub $a0, $a0, $t1 #subtract i
sll $a0, $a0, 2 #shift all left 2
addi $a0, $a0, 81 #intArr
add $a0, $a0, $sp #add from stack
addi $a1, $sp, 13 #entIntPrompt
jal GetOneIntByAddr
###########################################
endI1:
addi $t1, $t1, -1
FTestM1:
bgtz $t1, begFBodyM1
# ShowIntArray(intArr, valsToDo, initLab);
####################(3)####################
# intArr in register
addi $a0, $sp, 81
# valsToDo in register
#addi $a1, $sp, 77
lw $a1, 77($sp)
# initLab in register
addi $a2, $sp, 46
###########################################
jal ShowIntArray
# for (i = 0, j = valsToDo - 1; i
####################(3)####################
# li i, 0
li $t1, 0
# j = valsToDo - 1
lw $t2, 77($sp)
addi $t2, $t2, -1
###########################################
j FTestM2
begFBodyM2:
# SwapTwoInts(intArr + i, intArr + j);
####################(8)####################
#lw $a1, 81($sp)
sll $a1, $t1, 2
addi $a1, $a1, 81
add $a1, $a1, $sp
#sw $a1, 0($a3)
sll $a2, $t2, 2
addi $a2, $a2, 81
add $a2, $a2, $sp
#sw $a2, 0($t0)
###########################################
jal SwapTwoInts
addi $t1, $t1, 1 #++i
addi $t2, $t2, -1 #--j
FTestM2:
blt $t1, $t2, begFBodyM2 # i
# ShowIntArray(intArr, valsToDo, flipLab);
####################(3)####################
# load intArr into register
addi $a0, $sp, 81
# load valsToDo into register
lw $a1, 77($sp)
# load flipLab into register
addi $a2, $sp, 56
###########################################
jal ShowIntArray
# GetOneCharByAddr(&reply, dmPrompt);
####################(2)####################
# &reply in $sp
addi $a0, $sp, 76
# load up dmPrompt
addi $a1, $sp, 66
###########################################
jal GetOneCharByAddr
# }
# while (reply != 'n' && reply != 'N');
####################(1)####################
jal GetOneCharByAddr
###########################################
li $t0, 'n'
beq $v1, $t0, endWhileM1
li $t0, 'N'
bne $v1, $t0, begWBodyM1
endWhileM1: # extra helper label added
# return 0;
#}
addiu $sp, $sp, 109
li $v0, 10
syscall
################################################################################
#int GetOneIntByVal(const char prompt[])
#{
GetOneIntByVal:
# int oneInt;
# cout
li $v0, 4
syscall
# cin >> oneInt;
li $v0, 5
syscall
# return oneInt;
#}
jr $ra
################################################################################
#void GetOneIntByAddr(int* intVarToPutInPtr, const char prompt[])
#{
GetOneIntByAddr:
# cout
move $t0, $a0 # $t0 has saved copy of $a0 as received
move $a0, $a1
li $v0, 4
syscall
# cin >> *intVarToPutInPtr;
li $v0, 5
syscall
sw $v0, 0($t0)
#}
jr $ra
################################################################################
#void ValidateInt(int* givenIntPtr, int minInt, int maxInt, const char msg[])
#{
ValidateInt:
#################
# Register Usage:
#################
# $t0: copy of arg1 ($a0) as received
# $v1: value loaded from mem (*givenIntPtr)
#################
move $t0, $a0 # $t0 has saved copy of $a0 as received
# if (*givenIntPtr
# {
lw $v1, 0($t0) # $v1 has *givenIntPtr
bge $v1, $a1, ElseVI1
# cout
move $a0, $v1
li $v0, 1
syscall
move $a0, $a3
li $v0, 4
syscall
move $a0, $a1
li $v0, 1
syscall
li $a0, ' '
li $v0, 11
syscall
# *givenIntPtr = minInt;
sw $a1, 0($t0)
j endIfVI1
# }
# else
# {
ElseVI1:
# if (*givenIntPtr > maxInt)
# {
ble $v1, $a2, endIfVI2
# cout
move $a0, $v1
li $v0, 1
syscall
move $a0, $a3
li $v0, 4
syscall
move $a0, $a2
li $v0, 1
syscall
li $a0, ' '
li $v0, 11
syscall
# *givenIntPtr = maxInt;
sw $a2, 0($t0)
# }
endIfVI2:
# }
endIfVI1:
#}
jr $ra
################################################################################
#void ShowIntArray(const int array[], int size, const char label[])
#{
ShowIntArray:
#################
# Register Usage:
#################
# $t0: copy of arg1 ($a0) as received
# $a3: k
# $v1: value loaded from mem (*givenIntPtr)
#################
move $t0, $a0 # $t0 has saved copy of $a0 as received
# cout
move $a0, $a2
li $v0, 4
syscall
# int k = size;
move $a3, $a1
j WTestSIA
# while (k > 0)
# {
begWBodySIA:
# cout
sub $v1, $a1, $a3 # $v1 gets (size - k)
sll $v1, $v1, 2 # $v1 now has 4*(size - k)
add $v1, $v1, $t0 # $v1 now has &array[size - k]
lw $a0, 0($v1) # $a0 has array[size - k]
li $v0, 1
syscall
li $a0, ' '
li $v0, 11
syscall
# --k;
addi $a3, $a3, -1
# }
WTestSIA:
bgtz $a3, begWBodySIA
# cout
li $a0, ' '
li $v0, 11
syscall
#}
jr $ra
################################################################################
#void SwapTwoInts(int* intPtr1, int* intPtr2)
#{
SwapTwoInts:
#################
# Register Usage:
#################
# (fill in where applicable)
#################
# int temp = *intPtr1;
# *intPtr1 = *intPtr2;
# *intPtr2 = temp;
####################(4)####################
lw $t0, 0($a1) #put intPtr1 into temp
lw $t3, 0($a2) #put intPtr2 into temp2
sw $t3, 0($a1) #put intPtr2 where intPtr1 used to be
sw $t0, 0($a2) #put intPtr1 where intPtr2 used to be
###########################################
jr $ra
################################################################################
#void GetOneCharByAddr(char* charVarToPutInPtr, const char prompt[])
#{
GetOneCharByAddr:
#################
# Register Usage:
#################
# $t0: holder for saving 1st arg ($a0) received
#################
# cout
# cin >> *charVarToPutInPtr;
####################(7)####################
move $t0, $a0 #save intVarToPutInPtr ($a0) in $t0
# cout
move $a0, $a1 #save prompt ($a1) in $a0
li $v0, 4
syscall
# cin >> *charVarToPutInPtr;
li $v0, 12
syscall
move $v1, $v0
###########################################
#}
jr $ra
################################################################################
StrInitCode:
#################
# "bulky & boring" string-initializing code move off of main stage
################################################################################
li $t0, ' '
sb $t0, 0($sp)
li $t0, 'i'
sb $t0, 1($sp)
li $t0, 's'
sb $t0, 2($sp)
li $t0, ' '
sb $t0, 3($sp)
li $t0, 'b'
sb $t0, 4($sp)
li $t0, 'a'
sb $t0, 5($sp)
li $t0, 'd'
sb $t0, 6($sp)
li $t0, ','
sb $t0, 7($sp)
li $t0, ' '
sb $t0, 8($sp)
li $t0, 'm'
sb $t0, 9($sp)
li $t0, 'a'
sb $t0, 10($sp)
li $t0, 'k'
sb $t0, 11($sp)
li $t0, 'e'
sb $t0, 12($sp)
li $t0, ' '
sb $t0, 13($sp)
li $t0, 'i'
sb $t0, 14($sp)
li $t0, 't'
sb $t0, 15($sp)
li $t0, ' '
sb $t0, 16($sp)
li $t0, '\0'
sb $t0, 17($sp)
li $t0, 'i'
sb $t0, 18($sp)
li $t0, 'n'
sb $t0, 19($sp)
li $t0, 'i'
sb $t0, 20($sp)
li $t0, 't'
sb $t0, 21($sp)
li $t0, 'i'
sb $t0, 22($sp)
li $t0, 'a'
sb $t0, 23($sp)
li $t0, 'l'
sb $t0, 24($sp)
li $t0, ':'
sb $t0, 25($sp)
li $t0, ' '
sb $t0, 26($sp)
li $t0, '\0'
sb $t0, 27($sp)
li $t0, 'd'
sb $t0, 57($sp)
li $t0, 'o'
sb $t0, 58($sp)
li $t0, ' '
sb $t0, 59($sp)
li $t0, 'm'
sb $t0, 60($sp)
li $t0, 'o'
sb $t0, 61($sp)
li $t0, 'r'
sb $t0, 62($sp)
li $t0, 'e'
sb $t0, 63($sp)
li $t0, '?'
sb $t0, 64($sp)
li $t0, ' '
sb $t0, 65($sp)
li $t0, '\0'
sb $t0, 66($sp)
li $t0, 'f'
sb $t0, 67($sp)
li $t0, 'l'
sb $t0, 68($sp)
li $t0, 'i'
sb $t0, 69($sp)
li $t0, 'p'
sb $t0, 70($sp)
li $t0, 'p'
sb $t0, 71($sp)
li $t0, 'e'
sb $t0, 72($sp)
li $t0, 'd'
sb $t0, 73($sp)
li $t0, ':'
sb $t0, 74($sp)
li $t0, ' '
sb $t0, 75($sp)
li $t0, '\0'
sb $t0, 76($sp)
li $t0, 'v'
sb $t0, 81($sp)
li $t0, 'a'
sb $t0, 82($sp)
li $t0, 'l'
sb $t0, 83($sp)
li $t0, 's'
sb $t0, 84($sp)
li $t0, ' '
sb $t0, 85($sp)
li $t0, 't'
sb $t0, 86($sp)
li $t0, 'o'
sb $t0, 87($sp)
li $t0, ' '
sb $t0, 88($sp)
li $t0, 'd'
sb $t0, 89($sp)
li $t0, 'o'
sb $t0, 90($sp)
li $t0, '?'
sb $t0, 91($sp)
li $t0, ' '
sb $t0, 92($sp)
li $t0, '\0'
sb $t0, 93($sp)
li $t0, 'e'
sb $t0, 94($sp)
li $t0, 'n'
sb $t0, 95($sp)
li $t0, 't'
sb $t0, 96($sp)
li $t0, 'e'
sb $t0, 97($sp)
li $t0, 'r'
sb $t0, 98($sp)
li $t0, ' '
sb $t0, 99($sp)
li $t0, 'a'
sb $t0, 100($sp)
li $t0, 'n'
sb $t0, 101($sp)
li $t0, ' '
sb $t0, 102($sp)
li $t0, 'i'
sb $t0, 103($sp)
li $t0, 'n'
sb $t0, 104($sp)
li $t0, 't'
sb $t0, 105($sp)
li $t0, ':'
sb $t0, 106($sp)
li $t0, ' '
sb $t0, 107($sp)
li $t0, '\0'
sb $t0, 108($sp)
j endStrInit
AutoSave C OF : Assign03P1_LocVarRefSheet - Read-Only - Excel Sign in - 3 x File Home Insert Page Layout Formulas Data Review View / Tell me what you want to do e Share A X Cut Calibri E 5 A ZAutoSum ' AY O 11 AA === . Be Wrap Text General : A B # U Pe Copy - Fill - Paste BIU - I - A - A - * Format Painter === E A Merge & Center - $ - % 29 Conditional Fomat as CellInsert Delete Format Sort & Find & Formatting Table Styles Clear Filter - Select - Clipboard Font Alignment Number G Styles Cells Editing A1 A B C D E F G H I J K L M N O P Q R S T U V X Y 2 AA AB ACAC AE AF AG AH AI AJ AK AL AMAN (byte af) associated variable (t'byte of) associated from SPOuren (t'byte of) associated variable from SF(Ouren 3 SPEen (t'byse of associated vailable from SF(Ouren - SP (old) JakTCDO : " " N "N" : = 8 Eas= 8 o W 000 BESSAM 83 SNRNS Kie::43:656 IIIE 82 ERREIRA FR 2 5 883 888886 88888888888888888333333333 Mag R-SP (current) main Ready O - + 61% a R 4 10 11/21/2017 Y a Sa da 4:57 AM O Type here to search 0 0 e
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
