【问题标题】:decimal to binary in assembly mips汇编 mips 中的十进制到二进制
【发布时间】:2015-05-29 15:14:05
【问题描述】:

我正在尝试将十进制数转换为二进制数。我想将模数存储在一个数组中,然后打印结果。结果必须向后打印。到目前为止,这是我的代码。当它运行时,会出现一条消息说unaligned address,exception 5。到目前为止,这是我的代码

.data

prompt1: .asciiz "\n\n Please give the decimal number:"

prompt2: .asciiz "The binary number is:"

array: .space 32

linefeed: .asciiz "\n"

enterkey: .asciiz "Press any key to end program."




.text

main:

li $t0,0

li $t3,2

  li $s0, 0

  li $a0, 0

#t1-to hold number




li $v0, 4 #syscall to print string

la $a0, prompt1  #address of string to print

syscall

li $v0, 5 #syscall to read an integer

syscall

move $t1, $v0  #move the number to read into $t1


for: 

bge $s0, 8, end_for

  div $t1,$t3

  mflo $t1 #diversion

  mfhi $t1 #modulus

  sw $t1,array($t0) #save the number to read into array

  addi $t0,$t0,4

  addi $s0,$s0,1


  j for

end_for:



# print out a line feed

li $v0,4 # code for print_string

la $a0,linefeed # point $a0 to linefeed string

syscall # print linefeed



li $v0,1

move $a0,$t0

syscall 


# print out a line feed

li $v0,4 # code for print_string

la $a0,linefeed # point $a0 to linefeed string

syscall # print linefeed

# wait for the enter key to be pressed to end program

li $v0,4 # code for print_string

la $a0,enterkey # point $a0 to enterkey string

syscall # print enterkey

# wait for input by getting an integer from the user (integer is ignored)

li $v0,5 # code for read_int

syscall #get int from user --> returned in $v0

# All done, thank you!

li $v0,10 # code for exit

syscall # exit program

【问题讨论】:

  • 在 qtspim 调试器中运行代码并找出异常发生在程序中的哪个位置。

标签: arrays binary decimal mips qtspim


【解决方案1】:

我做了一些改进,但仍然没有。这次我想 它不会将值存储在数组中

.数据

prompt1: .asciiz "\n\n 请给出十进制数:"

prompt2: .asciiz "二进制数是:"

array:.align 2

  .space 32

换行:.asciiz "\n"

enterkey: .asciiz "Press any key to end program."

.文本

主要:

li $t0,0

li $t3,2

li $s0, 0

li $a0, 0

t1-保持号码

li $v0, 4 #syscall 打印字符串

la $a0, prompt1 #要打印的字符串的地址

系统调用

li $v0, 5 #系统调用读取整数

系统调用

move $t1, $v0 #将要读入的数字移入$t1

为:

bge $s0, 8, end_for

div $t1,$t3

mflo $t2 #div

mfhi $t4 #mod

移动$t1,$t2

li $v0,1 # print_int 的代码

move $a0,$t4 # 将结果放入 $a0

syscall #打印结果

sw $t4,array($t0) #保存要读入数组的数字

添加$t0,$t0,4

添加 $s0,$s0,1

j 为

end_for:

打印:

bge $s0, 8, end_forprint

lw $t4,array($t0) #保存要读入数组的数字

li $v0,1 # print_int 的代码

move $a0,$t4 # 将结果放入 $a0

syscall #打印结果

添加$t0,$t0,4

添加 $s0,$s0,1

j 用于打印

end_forprint:

打印换行

li $v0,4 # print_string 的代码

la $a0,linefeed # 将 $a0 指向换行字符串

syscall # 打印换行符

等待回车键结束程序

li $v0,4 # print_string 的代码

la $a0,enterkey #将$a0指向enterkey字符串

syscall #打印回车键

通过从用户那里获取一个整数来等待输入(整数被忽略)

li $v0,5 # read_int 的代码

syscall #get int from user --> 在 $v0 中返回

全部完成,谢谢!

li $v0,10 # 退出代码

syscall #退出程序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 2017-05-18
    • 2017-04-12
    • 1970-01-01
    • 2012-03-10
    • 2014-05-24
    • 2014-07-20
    相关资源
    最近更新 更多