尝试让 if 函数在 mips 中工作

发布于 2025-01-09 04:12:30 字数 945 浏览 0 评论 0原文

您好,刚开始使用 mars 程序和 mips,在读取用户输入及其地址后,我该如何使用它来执行特定的代码片段? 例如。

//get user input 
input = 1
if(input == 1)
//run this code

这就是我想做的,我可以解释它的最好方式(显然格式错误)。 这就是我到目前为止所拥有的

```
typeinfo: .asciiz  " 1 - Cls \n 2 - Row \n 3 - Column \n 4 - Triangle \n 5 - Exit\n\n"
typetext: .asciiz "Enter a number here for type: "


#prints the options
addi $v0, $zero, 4
la $a0, typeinfo
syscall 

#prints out the prompt
addi $v0, $zero, 4
la $a0, typetext
syscall 

#i want to be able to run this if user inputs "1"
###########################################################


lui $s0, 0x1004
addi $t8, $zero, 0x00ff
addi $t0, $s0, 0
lui $s1, 0x100C

drawPixel: 
    sw $t8, 0($t0)
    addi $t0, $t0, 4
    bne $t0, $s1, drawPixel

##########################################################
#gets the user input 
li $v0, 5
syscall

#move from $v0 to $t0
move $t1, $v0 

#ends program
li $v0, 10
syscall
```

Hello new to using mars program and mips in general how would i got about after reading user input and its in an adress how do i use that to execute specific pieces of code?
Eg.

//get user input 
input = 1
if(input == 1)
//run this code

this is what i want to do, best way i could explain it (obviously in wrong format).
This is what i have up to now

```
typeinfo: .asciiz  " 1 - Cls \n 2 - Row \n 3 - Column \n 4 - Triangle \n 5 - Exit\n\n"
typetext: .asciiz "Enter a number here for type: "


#prints the options
addi $v0, $zero, 4
la $a0, typeinfo
syscall 

#prints out the prompt
addi $v0, $zero, 4
la $a0, typetext
syscall 

#i want to be able to run this if user inputs "1"
###########################################################


lui $s0, 0x1004
addi $t8, $zero, 0x00ff
addi $t0, $s0, 0
lui $s1, 0x100C

drawPixel: 
    sw $t8, 0($t0)
    addi $t0, $t0, 4
    bne $t0, $s1, drawPixel

##########################################################
#gets the user input 
li $v0, 5
syscall

#move from $v0 to $t0
move $t1, $v0 

#ends program
li $v0, 10
syscall
```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情归归情 2025-01-16 04:12:30

以下是比较寄存器的方法(我使用的寄存器名称是任意的,您可以比较任意两个寄存器)

beq $t0,$t1,foo ;branch to label "foo" if t0 = t1

bne $t0,$t1,foo ;branch to label "foo" if t0 != t1


bltu $t0,$t1,foo ;branch to label "foo" if t0 < t1
blt $t0,$t1,foo 
;branch to label "foo" if t0 < t1 (numbers greater than 0x7FFFFFFF are treated as negative)

bleu $t0,$t1,foo ;branch to label "foo" if t0 <= t1
ble $t0,$t1,foo 
;branch to label "foo" if t0 <= t1 (numbers greater than 0x7FFFFFFF are treated as negative)

bgeu $t0,$t1,foo ;branch to label "foo" if t0 >= t1
bge $t0,$t1,foo
;branch to label "foo" if t0 >= t1 (numbers greater than 0x7FFFFFFF are treated as negative)

bgtu $t0,$t1,foo ;branch to label "foo" if t0 > t1
bgt $t0,$t1,foo
;branch to label "foo" if t0 > t1 (numbers greater than 0x7FFFFFFF are treated as negative)

但是,有时,放置代码的顺序并不总是简单的。如果您想写:

input = 1;
if(input == 1)
  // do something, else do nothing

实际上反转分支条件更容易,如下所示:

li $t1,1 ;constant one for comparing
li $t0,1 ;input
bne $t0,$t1, doNothing
; do something. This will be skipped if $t0 != $t1
doNothing:

Here are the ways you can compare registers (the register names I use are arbitrary, you can compare any two registers)

beq $t0,$t1,foo ;branch to label "foo" if t0 = t1

bne $t0,$t1,foo ;branch to label "foo" if t0 != t1


bltu $t0,$t1,foo ;branch to label "foo" if t0 < t1
blt $t0,$t1,foo 
;branch to label "foo" if t0 < t1 (numbers greater than 0x7FFFFFFF are treated as negative)

bleu $t0,$t1,foo ;branch to label "foo" if t0 <= t1
ble $t0,$t1,foo 
;branch to label "foo" if t0 <= t1 (numbers greater than 0x7FFFFFFF are treated as negative)

bgeu $t0,$t1,foo ;branch to label "foo" if t0 >= t1
bge $t0,$t1,foo
;branch to label "foo" if t0 >= t1 (numbers greater than 0x7FFFFFFF are treated as negative)

bgtu $t0,$t1,foo ;branch to label "foo" if t0 > t1
bgt $t0,$t1,foo
;branch to label "foo" if t0 > t1 (numbers greater than 0x7FFFFFFF are treated as negative)

Sometimes, the order in which you put your code isn't always straightforward, however. In the event you want to write:

input = 1;
if(input == 1)
  // do something, else do nothing

It's actually easier to reverse the branch condition, like so:

li $t1,1 ;constant one for comparing
li $t0,1 ;input
bne $t0,$t1, doNothing
; do something. This will be skipped if $t0 != $t1
doNothing:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文