Ⅰ 求一段 MIPS 汇编代码 计算某一个正整数是否是另一个正整数的倍数
先看看C代码:
#include <stdio.h>
main()
{
int a, b;
retry_input:
printf("input A and B: ");
scanf("%d %d", &a, &b);
if(a==0 || b==0)
return;
if(a<0 || b<0)
goto retry_input;
if(a%b==0)
printf("A is a multiple of B ");
else
printf("A is not a multiple of B ");
return;
}
然后直接用GCC -S生成汇编代码:
.file 1 "111.c"
.section .mdebug.abi32
.previous
.gnu_attribute 4, 3
.abicalls
.option pic0
.rdata
.align 2
$LC0:
.ascii "input A and B: 00"
.align 2
$LC1:
.ascii "%d %d 00"
.align 2
$LC2:
.ascii "A is a multiple of B 00"
.align 2
$LC3:
.ascii "A is not a multiple of B 00"
.section .text.main,"ax",@progbits
.align 2
.globl main
$LFB0 = .
.cfi_startproc
.set nomips16
.ent main
.type main, @function
main:
.frame $fp,40,$31 # vars= 8, regs= 2/0, args= 16, gp= 8
.mask 0xc0000000,-4
.fmask 0x00000000,0
.set noreorder
.set nomacro
addiu $sp,$sp,-40
.cfi_def_cfa_offset 40
sw $31,36($sp)
sw $fp,32($sp)
move $fp,$sp
.cfi_offset 30, -8
.cfi_offset 31, -4
.cfi_def_cfa_register 30
$L2:
lui $2,%hi($LC0)
addiu $4,$2,%lo($LC0)
jal puts
nop
lui $2,%hi($LC1)
addiu $3,$2,%lo($LC1)
addiu $2,$fp,28
move $4,$3
addiu $3,$fp,24
move $5,$3
move $6,$2
jal __isoc99_scanf
nop
lw $2,24($fp)
beq $2,$0,$L9
nop
lw $2,28($fp)
beq $2,$0,$L9
nop
$L4:
lw $2,24($fp)
bltz $2,$L2
nop
lw $2,28($fp)
bltz $2,$L2
nop
lw $3,24($fp)
lw $2,28($fp)
div $0,$3,$2
teq $2,$0,7
mfhi $2
bne $2,$0,$L6
nop
lui $2,%hi($LC2)
addiu $4,$2,%lo($LC2)
jal puts
nop
j $L8
nop
$L6:
lui $2,%hi($LC3)
addiu $4,$2,%lo($LC3)
jal puts
nop
j $L8
nop
$L9:
nop
$L8:
move $sp,$fp
lw $31,36($sp)
lw $fp,32($sp)
addiu $sp,$sp,40
j $31
nop
.set macro
.set reorder
.end main
.cfi_endproc
$LFE0:
.size main, .-main
.ident "GCC: 4.6.4"
Ⅱ 请问怎么写MIPS汇编语言
字符串度,存入寄存器计数,字符址,字符移新串第位置,址减,字串度计数器减,判断计数器否0,0,重复移字符,零退循环
按顺序显示字符串式显示
Ⅲ MIPS汇编程序
现看的,可能不是太好,不过倒是能用-x-b
.data
str0: .asciiz "Primes in 1000:\n"
str1: .asciiz ",\n"
.text
.globl __start
__start:
la $a0, str0
li $v0, 4
syscall
li $t0, 2
j __test0
__l0:
li $t1, 1
li $t2, 2
j __test1
__l1:
div $t0, $t2
mfhi $t4
bne $t4, $0, __br1
move $t1, $0
__br1:
addi $t2, 1
__test1:
bne $t2, $t0, __l1
beq $t1, $0, __br0
move $a0, $t0
li $v0, 1
syscall
la $a0, str1
li $v0, 4
syscall
__br0:
addi $t0, 1
__test0:
li $t4, 100
bne $t0, $t4, __l0
li $v0, 10
syscall
----
有个事忘说了,这个真机上可能跑不了,那个模拟器模拟delay slot我没弄明白。
Ⅳ 求一段完整的MIPS汇编代码
代码如下:
(这个是网络得到的 )
~/ vi Hello.c
"Hello.c" [New file]
/* Example to illustrate mips register convention
* -Author: BNN
* 11/29/2001
*/ int addFunc(int,int);
int subFunc(int); void main()
{ int x,y,z;
x= 1;
y=2;
z = addFunc(x,y);
}
int addFunc(int x,int y)
{
int value1 = 5;
int value2; value2 = subFunc(value1);
return (x+y+value2); } int subFunc(int value)
{
return value--;
}
反汇编代码后的代码:
/* main Function */
0000000000000000 :
/*create a stack frame by moving the stack pointer 8
*bytes down and meantime update the sp value
*/
0: 27bdfff8 addiu $sp,$sp,-8
/* Save the return address to the current sp position.*/
4: afbf0000 sw $ra,0($sp)
8: 0c000000 jal 0
/* nop is for the delay slot */
c: 00000000 nop
/* Fill the argument a0 with the value 1 */
10: 24040001 li $a0,1
/* Jump the addFunc */
14: 0c00000a jal 28
/* NOTE HERE: Why we fill the second argument
*behind the addFunc function call?
* This is all about the "-O1" compilation optimizaiton.
* With mips architecture, the instruciton after jump
* will also be fetched into the pipline and get
* exectuted. Therefore, we can promise that the
* second argument will be filled with the value of
* integer 2.
*/
18: 24050002 li $a1,2
/*Load the return address from the stack pointer
* Note here that the result v0 contains the result of
* addFunc function call
*/
1c: 8fbf0000 lw $ra,0($sp)
/* Return */
20: 03e00008 jr $ra
/* Restore the stack frame */
24: 27bd0008 addiu $sp,$sp,8 /* addFunc Function */
0000000000000028 :
/* Create a stack frame by allocating 16 bytes or 4
* words size
*/
28: 27bdfff0 addiu $sp,$sp,-16
/* Save the return address into the stack with 8 bytes
* offset. Please note that compiler does not save the
* ra to 0($sp).
*Think of why, in contrast of the previous PowerPC
* EABI convention
*/
2c: afbf0008 sw $ra,8($sp)
/* We save the s1 reg. value into the stack
* because we will use s1 in this function
* Note that the 4,5,6,7($sp) positions will then
* be occupied by this 32 bits size register
*/
30: afb10004 sw $s1,4($sp)
/* Withe same reason, save s0 reg. */
34: afb00000 sw $s0,0($sp)
/* Retrieve the argument 0 into s0 reg. */
38: 0080802d move $s0,$a0
/* Retrieve the argument 1 into s1 reg. */
3c: 00a0882d move $s1,$a1
/* Call the subFunc with a0 with 5 */
40: 0c000019 jal 64
/* In the delay slot, we load the 5 into argument a0 reg
*for subFunc call.
*/
44: 24040005 li $a0,5
/* s0 = s0+s1; note that s0 and s1 holds the values of
* x,y, respectively
*/
48: 02118021 ad $s0,$s0,$s1
/* v0 = s0+v0; v0 holds the return results of subFunc
*call; And we let v0 hold the final results
*/
4c: 02021021 ad $v0,$s0,$v0
/*Retrieve the ra value from stack */
50: 8fbf0008 lw $ra,8($sp)
/*!!!!restore the s1 reg. value */
54: 8fb10004 lw $s1,4($sp)
/*!!!! restore the s0 reg. value */
58: 8fb00000 lw $s0,0($sp)
/* Return back to main func */
5c: 03e00008 jr $ra
/* Update/restore the stack pointer/frame */
60: 27bd0010 addiu $sp,$sp,16 /* subFunc Function */
0000000000000064 :
/* return back to addFunc function */
64: 03e00008 jr $ra
/* Taking advantage of the mips delay slot, filling the
* result reg v0 by simply assigning the v0 as the value
*of a0. This is a bug from my c source
* codes--"value--". I should write my codes
* like "--value", instead.
68: 0080102d move $v0,$a0
Ⅳ mips 汇编语言计算
wind", the poet's lament regard
Ⅵ 希望大神解释一下关于汇编语言MIPS
a:.word-12
这条指令。是分配一个word类型的空间给变量a,并初始化成-12。其具体的地址,在编程的时候“不知道”。必须在整个汇编程序汇编完之后,连接,并可能“重定位”,这些变量的地址才会真正知道。
编译时,开发工具通常都会生成类似于“.map”和“.lst”之类的文件,里面会有详细的变量和函数之类的地址信息。具体文件,得查阅MIPS汇编程序开发工具的相关手册(抱歉,我手边没有)。
有问题继续交流,谢谢。
Ⅶ 使用MIPS汇编编写一个计算1-100的和并打印输出的程序
PRG1:
MOV sum, 0
MOV CX, 100
LP:
TEST CX, 1
JE NEXT
ADD sum, CX
NEXT:
LOOP LP
RET
END
Ⅷ mips汇编语言中如何调用子函数
.test
addi $a0,$zero,2 #a0=x
addi $a1,$zero,1 #a1=y
jal sub
#return here
sub:add $v0,$a0,$a1 #function
jr $ra #return
这是jal函数调用的方法
返回的地址即是jal的下一条指令地址
Ⅸ 求一段完整的MIPS汇编语言,最好是加法的,或者是将输入的数字转换为字符
.global main
.data
str0:
.ascii "Input two integers: \000"
str1:
.ascii "%d%d\000"
str2:
.ascii "The sum of %d and %d is %d. \012\000"
add:
addiu $sp, $sp, -12
sw $ra, 8($sp)
sw $fp, 4($sp)
move $fp, $sp
add $v0, $a0, $a1
move $sp, $fp
lw $fp, 4($sp)
lw $ra, 8($sp)
addiu $sp, $sp, 12
j $ra
nop
main:
addiu $sp, $sp, -40
sw $ra, 36($sp)
sw $fp, 32($sp)
move $fp, $sp
la $a0, str0
jal printf
nop
la $a0, str1
addiu $a1, $fp, 28
addiu $a2, $fp, 24
jal __isoc99_scanf
nop
lw $a0, 28($fp)
lw $a1, 24($fp)
jal add
nop
la $a0, str2
lw $a1, 28($fp)
lw $a2, 24($fp)
move $a3, $v0
jal printf
nop
move $sp, $fp
lw $fp, 32($sp)
lw $ra, 36($sp)
addiu $sp, $sp, 40
j $ra
nop
Ⅹ C语言切换成MIPS汇编语言,B[8] = A[i-j];
sub $t0, $s3, $s4 #i-j
sll $t0, $t0, 2 #$t0=4(i-j),因为一个字占四个字节,所以i-j个字要4倍,mips中是字节地址
add $t0, $t0, $s6 #¥t0=A的地址加上$t0,即是A[i-j]的地址
lw $t1, 0($t0) # 按相应地址从存储器复制值,赋给$t1
sw $t1,32($s7) #8个字,32个字节,所以加上32,这一步为将$t1中的值存储到存储器相应地址,这个地址就是&B[8]了,即B的基础地址加上字节数!
此时应该完成了