반응형
컴퓨터 구조 / 2003년 1학기 / 김지홍 교수님
#################################################################
# #
# 2001-12204 이준희 #
# Problem 2. WriteBackwards #
# bare mode version #
# there are some no-operation instructions #
# following branch/jump instructions #
# for delayed branch/jump #
# data segement is fixed at 0x10010000 #
# tested at junebug by spim bare mode #
# ./spim -bare -file 200112204_P2.s #
# #
# [pseudocode] #
# WriteBackwards(A[]) #
# if A[0] == 0 #
# return; #
# WriteBackwards(A[] + 1); #
# print A[0]; #
# return; #
# #
#################################################################
.data 0x10010000
msg1: .asciiz "Input string is: " # 4097
msg2: .asciiz "The reverse is: " # 4097 + 18
newline: .asciiz "\n" # 4097 + 35
#input_start
input_string: .asciiz ".sllih hgih no wolb sdniw hgiH"
#input_end
.text
WriteBackwards:
lb $t0, 0($a0) # load most left character
beq $t0, $zero, WB_end # if it is null("\0"), branch to WB_end
ori $t0, $t0, 0 # nop
subu $sp, $sp, 8
sw $ra, 4($sp) # save return address
sw $a0, 8($sp) # save A[]
addi $a0, $a0, 1 # A[] = A[] + 1
jal WriteBackwards
ori $t0, $t0, 0 # nop
lw $ra, 4($sp) # restore return address
lw $a0, 8($sp) # restore A[]
addu $sp, $sp, 8
sb $zero, 1($a0) # byte at A[]+1 = 0 to notify the end of string
ori $v0, $zero, 4 # v0 = 4
syscall # print string (actually print one character)
WB_end: jr $ra # return
main:
lui $a0, 4097 # address of msg1
ori $v0, $zero, 4
syscall # print string
ori $a0, $a0, 37 # address of input_string
syscall
addi $a0, $a0, -2 # address of newline
syscall
addi $a0, $a0, -17 # address of msg2
syscall
addi $a0, $a0, 19 # address of newline
jal WriteBackwards
ori $t0, $t0, 0 # nop
lui $a0, 4097
ori $a0, $a0, 35 #address of newline
syscall
#################################################################
# #
# 2001-12204 이준희 #
# Problem 2. WriteBackwards #
# bare mode version #
# there are some no-operation instructions #
# following branch/jump instructions #
# for delayed branch/jump #
# data segement is fixed at 0x10010000 #
# tested at junebug by spim bare mode #
# ./spim -bare -file 200112204_P2.s #
# #
# [pseudocode] #
# WriteBackwards(A[]) #
# if A[0] == 0 #
# return; #
# WriteBackwards(A[] + 1); #
# print A[0]; #
# return; #
# #
#################################################################
.data 0x10010000
msg1: .asciiz "Input string is: " # 4097
msg2: .asciiz "The reverse is: " # 4097 + 18
newline: .asciiz "\n" # 4097 + 35
#input_start
input_string: .asciiz ".sllih hgih no wolb sdniw hgiH"
#input_end
.text
WriteBackwards:
lb $t0, 0($a0) # load most left character
beq $t0, $zero, WB_end # if it is null("\0"), branch to WB_end
ori $t0, $t0, 0 # nop
subu $sp, $sp, 8
sw $ra, 4($sp) # save return address
sw $a0, 8($sp) # save A[]
addi $a0, $a0, 1 # A[] = A[] + 1
jal WriteBackwards
ori $t0, $t0, 0 # nop
lw $ra, 4($sp) # restore return address
lw $a0, 8($sp) # restore A[]
addu $sp, $sp, 8
sb $zero, 1($a0) # byte at A[]+1 = 0 to notify the end of string
ori $v0, $zero, 4 # v0 = 4
syscall # print string (actually print one character)
WB_end: jr $ra # return
main:
lui $a0, 4097 # address of msg1
ori $v0, $zero, 4
syscall # print string
ori $a0, $a0, 37 # address of input_string
syscall
addi $a0, $a0, -2 # address of newline
syscall
addi $a0, $a0, -17 # address of msg2
syscall
addi $a0, $a0, 19 # address of newline
jal WriteBackwards
ori $t0, $t0, 0 # nop
lui $a0, 4097
ori $a0, $a0, 35 #address of newline
syscall
반응형