導航:首頁 > 編程語言 > mips匯編程序

mips匯編程序

發布時間:2023-01-29 19:13:15

Ⅰ 求一段 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 %d00"
.align 2
$LC2:
.ascii "A is a multiple of B00"
.align 2
$LC3:
.ascii "A is not a multiple of B00"
.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的基礎地址加上位元組數!
此時應該完成了

閱讀全文

與mips匯編程序相關的資料

熱點內容
解壓做食物的小視頻 瀏覽:756
pdf怎麼單獨設置文件夾 瀏覽:472
業務邏輯程序員 瀏覽:657
addto新建文件夾什麼意思 瀏覽:160
有伺服器地址怎麼安裝軟體 瀏覽:659
安卓如何完全清除數據 瀏覽:690
安卓安卓證書怎麼信任 瀏覽:53
伺服器被攻擊如何解決 瀏覽:221
學霸變成程序員 瀏覽:881
c語言編譯錯誤fatalerror 瀏覽:441
ipv4內部伺服器地址怎麼分配 瀏覽:463
java線程安全的方法 瀏覽:950
重復命令畫梯形 瀏覽:164
在疫情就是命令 瀏覽:328
自己搭建一個什麼伺服器好玩 瀏覽:253
java基礎馬士兵 瀏覽:823
完美世界手游如何查看伺服器 瀏覽:859
光遇安卓與ios什麼時候互通 瀏覽:598
js如何運行時編譯 瀏覽:917
引力app在哪裡下載 瀏覽:609