Mips程序无法打印正确的字符串

时间:2018-12-02 22:59:37

标签: mips converters roman-numerals

我正在创建一个程序来转换罗马数字。我正在输入字母(罗马数字),并且输出应为相应的整数。当我输入罗马数字时,无论数字是多少,都说数字为0。我认为这是跳过rom函数,只是直接打印出一些内容,但我似乎无法弄清楚为什么这样做。

  #Program to convert Roman numerals
  #Written by Naomi Gebremariam

  .data
  .align    2 
fprompt: .asciiz "Enter a Roman number: "
fanswer: .asciiz " The arabic notation of the roman numeral you entered is = \n"
  #----------------------------------Usual stuff at beginning of main---------------------------------------------------
  .text
  .globl main

  #takes the user input and puts it into registers, makes a call to check then prints result
main:               

  #get roman numeral inpuut from user

  li $s1,'I'     #load I into s0
  li $s2,'V'             
  li $s3,'X' 
  li $s4,'L'
  li $s5,'C'
  li $s6,'D'
  li $s7,'M'

  la $a0,fprompt
  li $v0,4
  syscall
  li $v0,51
  syscall

  jal rom

  #a0 has n (romnum)


  #print result   
  move $v0,$t1
  la $a0,fanswer         
  li $v0,1       
  syscall            
  li $v0,10          
  syscall   

  #have to store these so that the sequence can be calculated


  #-------------------------Function to check how many symbols are entered---------------------------------------


rom:     
  addi $sp, $sp, -8 #allocate stack space
  sw $ra, 0($sp) #store return address
  sw $s0, 4($sp)
  #------------function body--------------------------      

  #load roman numerals into registers for comparison
  move $s0,$v0
  beq $s1,$s0,one
  beq $s2,$a0,five
  beq $s3,$s0,ten 
  beq $s4,$s0,fifty
  beq $s5,$s0,hundred
  beq $s6,$s0,fivehun
  beq $s7,$s0,thousand


one:  
  li $t1,1

five:
  li $t1,5
  jr $ra

ten: 
  li $t1,10 #load 10 into result
  jr $ra

fifty:
  li $t1,50
  jr $ra

hundred:
  li $t1,100

fivehun: 
  li $t0,500

thousand:
  li $t1,1000

done:          
  lw $ra, 0($sp)
  lw $s0, 4($sp)
  addi $sp, $sp, 8 
  jr $ra 

1 个答案:

答案 0 :(得分:1)

对于初学者来说,这些行:

li $v0,51
syscall

要读取一个整数并将其放入a0,但是您想读取一个包含罗马数字的字符串。

因此,您需要在某个地方存储该字符串,然后(如果要处理单个字符号)需要从该字符串中取出第一个字符。

以后:

hundred:
  li $t1,100

fivehun: 
  li $t0,500

thousand:
  li $t1,1000

与这些标签有关的任何事情最终都会将t1设置为1000,因为代码只是进入下一条语句,这与您对一个,数十个等所做的不同。