MIPS汇编范例

合集下载

【参考文档】mips汇编范例-范文模板 (13页)

【参考文档】mips汇编范例-范文模板 (13页)

本文部分内容来自网络整理,本司不为其真实性负责,如有异议或侵权请及时联系,本司将立即删除!== 本文为word格式,下载后可方便编辑和修改! ==mips汇编范例篇一:MIPS汇编范例mips汇编语言之实现swap函数收藏此程序用来交换两个整数已在pcspim下编译通过#########################################################programed by stevie zou #### purpose:to swap two values ######10-15-201X######### ############################### text segment ###############.text.globl mainmain: la$t0, number #读取两个整数并放入寄存器$t1,$t2lw$t1, 0($t0)lw$t2, 4($t0)li$v0, 4#打印msg1la$a0, msg1li$v0, 1 #打印转换前$t1中的值move $a0, $t1syscallli$v0, 4 #打印msg2la$a0, msg2syscallli$v0, 1#打印转换前$t2中的值move $a0, $t2syscallmove $t3, $t1#关键部分,在寄存器间move数据move $t1, $t2move $t2, $t3li$v0, 4 #打印msg3la$a0, msg3syscallli$v0, 1 #打印转换后$t1中的值move $a0, $t1syscallli$v0, 4#打印换行符 /nla$a0, msg4syscallli$v0, 1#打印转换后$t2中的值move $a0, $t2########### data segment ##############.datanumber: .word 12,34msg1: .asciiz "the first number is:\n"msg2: .asciiz "\nthe second number is:\n"msg3: .asciiz "\nnow they are swapped as:\n"msg4: .ascii"\n"## end of file程序运行结果为:本文来自CSDN博客,转载请标出处明:mips汇编简单实例——一个小计算器收藏其实开始的时候一直在看 mips的指令格式,看了、忘了,没什么效果。

mips汇编语言程序设计实例

mips汇编语言程序设计实例
la$a0,a
syscall
jmain
.asciiz
表示输出一个字符串,以\n换行符结尾。
.data
定义变量
a:.asciizAlpha。
等于:
Stringa=Alpha;
MIPS汇编程序设计
实验目的
1.掌握QTSPIM的调试技术
2.了解MIPS汇编语言与机器语言之间的对应关系。
3.掌握MIPS汇编程序设计
算法设计
.data
#定义用户数据段
Z:.space200.text
#定义用户程序段
main:
#给K,Y分配寄存器并赋初值
#给数组Z[K]分配寄存器
#计算Y-16*(K/4+210)并将结果存入一个寄存器
#将上述寄存器中的结果写入Z[K]
done:
li$v0,10
syscall
源程序代码
.data#定义用户数据段
z:.space200
.text
main:
la$s0,z#$s0=addrz
li$t0,0#$s1=k=0
li$t1,56#$s2=y=56
loop:
slTI$t2,$t0,50#判断k是否于50
beq$t2,$0,done#当k大于等于50,跳转
srl$t3,$t0,2#k/4
addi$t3,$t3,210#k/4+210
4.了解C语言语句与汇编指令之间的关系
5.熟悉常见的MIPS汇编指令
6.掌握程序的内存映像
实验任务
用汇编程序实现以下伪代码:要求采用移位指令实现乘除法运算。
intmain()
{intK,Y
intZ[50]
Y=56;

MIPS汇编语言

MIPS汇编语言

无符号数比较小于时置位 sltu $s1, $s2, $s3 比较小于立即数时置位 sltiu $s1, $s2, 100
j 2500 无 跳转 条 件 跳转至寄存器所指的位置 jr $ra 跳 转 跳转并链接 jal 2500
MIPS汇编语言 类 指令 别 加 减 加立即数 无符号加 无符号减 举例 add $s1, $s2, $s3 sub $s1, $s2, $s3 addi $s1, $s2, 100 addu $s1, $s2, $s3 subu $s1, $s2, $s3 含义 $s1 = $s2 + $s3 $s1 = $s2 - $s3 $s1 = $s2 + 100 $s1 = $s2 + $s3 $s1 = $s2 - $s3 $s1 = $s2 + 100 $s1 = $epc Hi, Lo = $s2 × $s3 Hi, Lo = $s2 × $s3 Lo = $s2 / $s3 Hi = $s2 mod $s3 Lo = $s2 / $s3 Hi = $s2 mod $s3 $s1 = Hi $s1 = Lo $s1 = Memory[$s2 + 20] Memory[$s2 + 20] = $s1 $s1 = Memory[$s2 + 20] Memory[$s2 + 20] = $s1 $s1 = Memory[$s2 + 20] Memory[$s2 + 20] = $s1 $s1 = Memory[$s2 + 20] Memory[$s2 + 20] = $s1; $s1 = 0或1 $s1 = 100 * 2^16 $s1 = $s2 & $s3 $s1 = $s2 | $s3 $s1 ~($s2 | $s3) $s1 = $s2 & 100 $s1 = $s2 | 100 $s1 = $s2 << 10 $s1 = $s2 >> 10 备注 三个操作数;检测溢出 三个操作数;检测溢出 加常数,检测溢出 三个操作数;不检测溢出 三个操作数;不检测溢出 加常数;不检测溢出 复制异常PC到专用寄存器 64位有符号积存在Hi,Lo中 64位无符号积存在Hi,Lo中 Lo = 商, Hi = 余 无符号商和余数 用来获得Hi的拷贝 用来获得Lo的拷贝 将一个字从内存取到寄存器中 将一个字从寄存器中存到内存中 将半个字从内存取到寄存器中 将半个字从寄存器中存到内存中 将一个字节从内存取到寄存器中 将一个字节从寄存器中存到内存中 取字作为原子交换的前半部分 存字作为原子交换的后半部分 取立即数并放在高16位 三个寄存器操作数;按位与 三个寄存器操作数;按位或 三个寄存器操作数;按位或非 和常数按位与 和常数按位或 根据常数左于时置位 件 跳 转 小于立即数时置位

基于MIPS指令集的汇编程序(spim仿真)——快速排序和二分搜索

基于MIPS指令集的汇编程序(spim仿真)——快速排序和二分搜索

Report for MIPS Assembler ProgramsQuick Sort and Binary SearchHAO Cong1. IntroductionIn this program, I implemented two major functions: quick sort and binary search. First, a list of integers is given by user, and the integers are sorted in ascending order using quick sort method. Then the program search for the particular integer given by user, using binary search, and provides its position. Figure 1 shows the basic structure of the program.Figure 1: Basic Structure of Program2. Implementation2.1 Quick Sort2.1.1 Brief Introduction to Quick Sort①Quicksort sorts by employing a divide and conquer strategy to divide a list into two sub-lists.The steps are:1. Pick an element, called a pivot, from the list.2. Reorder the list so that all elements with values less than the pivot comebefore the pivot, while all elements with values greater than the pivot comeafter it (equal values can go either way). After this partitioning, the pivot is inits final position. This is called the partition operation.3. Recursively sort the sub-list of lesser elements and the sub-list of greaterelements.The base case of the recursion is list of size zero or one, which never need to be sorted.2.1.2 Pseudo code2.1.3 Implementation of Quick SortEven having written C code, it’s not easy to change into assembler code. The most difficult part is applying the recursive method. We have to take care of building Stack Frame and storing registers. Now I’d like to take several parts of the code to make detailedexplanation.Firstly,we claim 1024bytes in memory, in order to store the integers to be sorted. Use .data to put them in data segment. From now on, all operations on integers are based on memory.Here, we begin to call the function of quick sort. In MIPS, when calling other functions, parameters are stored in $a0 - $a3. So we put the begin index and end index in $a0 and $a1, then the parameters are transferred to sub-functions.In order to jump, we used instruction jal. Using this, the return address can be stored in $ra atomatically. So when caller ends, PC pointer can return to the next instruction of callee after jal to continue.This is the end condition of recursive. If the left list has only one element, the program should return and end recursive. In this case we don’t need to build a stack frame for callee. It can save execution time.At the beginning of a callee, it must:a) Create a stack frame, by subtracting the frame size from the stack pointer ($sp). In MIPS, the minimum stack frame size is 32 bytes, so even if the program doesn't need all of this space, stack frames still should be made this large.b) Save any callee-saved registers ($s0 - $s7, $fp, $ra) which are used by the callee. In this program, only $fp and $ra need to be saved.c) Set the frame pointer to the stack pointer, plus the frame size.Here we calculate the pivot value: the average of first and last element.Given middle value, we begin to compare from both beginning and ending of the list. From the beginning, we try to find the first integer that is lager than pivot value; from the end, we try to find the first integer that is smaller than pivot value. If the two pointers haven’t touch, do swap. Otherwise, do partition.The above figure shows the branch flow. In order to make it clear, I hided detailed code.Besides, I used instruction b/bgt/ble/blt/bge here, instead of jump instructions. Their functions are similar, but there’re also some differences between them. One is jump instructions will automatically store the return address in $ra, but branch instructions don’t. Here, we only need to implement the function of if-else, so branch instruction is OK.Here is recursive call. Since the function call will change the values in registers, the values must be pushed in stack before calling sub-functions and be popped after calling. So the sw instruction is used to preserve and lw is used to restore.In MIPS, there’s a common custom, that is, $t0-$t7 is called Temporary Register, and $s0-s7 is called Saved Register. In general, the caller should take charge of keep the value of $t0-$t7, if necessary, and the callee has the responsibility to save $s0-$s7. In other words, $t0-$t7 is “temporary” to caller, because callee may modifies it, so caller has to preserve them; but $s0-$s7 is “saved” to caller, because callee will save them and restore them before return, so caller doesn’t need to care. Keeping this custom does help to create good program, I think.At last, before function returns, it restore the $ra, $fp and other registers such as$s0-$s7, and jump to return address.So in this way, quick sort is implemented successfully. The sorted integers are kept in memory in order and can be output by using syscall. Input and output is quite easy, so I won’t introduce here.2.1 Binary Search2.2.1 Brief Introduction to Binary SearchBinary search is one of the searching algorithms, which is widely used in finding a given number in a list of sorted numbers. It’s time complexity is O(logn) and memory complexity is O(n).At the beginning, we pick up the number in the middle and compare it with the given one. If the given one is larger than the middle one, we go on searching the back half of the list, and if the given one is smaller than the middle one, we go on searching the front half of the list. Repeat this procedure until the number is found or cannot divide any more. In this way, we throw half of the numbers each comparison. So it’s called binary search.2.2.2 Implementation of Binary SearchCompared with quick sort, binary search is easier and doesn’t need recursive. However, I will also make a brief explanation on binary search.Here we begin binary search. The function needs two parameters: left index and right index.In the above segment, $t0 is the given number, and $t4 is the middle number. We can clearly see that if the given one is larger than the middle one, we go on searching the back half, and if the given one is smaller than the middle one, we go on searching the front half. If equal, target number is found, and program jump to print the result.If the target number is supposed in front half, we should move the right pointer to middle, otherwise, if it’s supposed in back half, we move the left pointer to middle.Of course, if at last there remains only two numbers and cannot be divided any more, compare the target number with both of them. If still not hit, just tell that the target number cannot be found.In this way, binary search is implemented successfully.3. Execution ResultsThe following figures show the execution results of this program. The first figure shows the whole picture while running. Figure 2 shows detailed result, including both sorting and searching. And figure 3 shows that the program can successfully deal with some complex situations, such as repeating numbers.Figure 1: Whole picture when runningFigure 2: More detailed resultFigure 3: Successfully deals with repeating numbers4. Code## HAO Cong -- 2010/12/15 - 2010/12/19## QuickSort_BinarySearch.asm -- A program for quick sort and binary search.dataarray: .space 1024num_of_integer: .word 0comment_msg: .asciiz "This is a program for quick sort and binary search.\n"input_msg: .asciiz "Please input the integers(-1 to quit):"search_msg: .asciiz "Please input a target integer:"not_found: .asciiz "Sorry, the integer does not exist."pos: .asciiz "Position: "thanks: .asciiz "Thanks for using!\n"newline: .asciiz "\n"whitespace: .asciiz " ".text.globl mainmain:la $a0, comment_msg # print the promoting message to screenli $v0, 4syscallmove $t7, $zero # t7 stores the number of integersla $t6, array # get base address of array and store in t6input:la $a0, input_msg # print the input message for userli $v0, 4syscallli $v0, 5 # read an integer from usersyscallli $t0, -1beq $v0, $t0, end_input # end input if integer is -1move $t0, $t7mul $t0, $t0, 4 # calculate offset addressaddu $t1, $t0, $t6 # calculate the actual address of the integer in memorysw $v0, ($t1) # store the integer to memoryaddu $t7, $t7, 1 # total number plus oneb input # go on inputend_input:sw $t7, num_of_integer # store the number of integers into memoryquick_sort_begin:li $a0, 0 # $a0: start indexsubu $a1, $t7, 1 # $a1: end indexjal quick_sort # call quick_sortoutput:lw $t1, num_of_integer # get the number of integers and keeps in $t1 li $t2, 0out_loop:beqz $t1, search # if $t1 decrease to zero, end the programsubu $t1, $t1, 1 # minus t1 by onemul $t3, $t2, 4 # calculate the integer's addressla $t4, arrayaddu $t4, $t3, $t4lw $a0, ($t4) # print one integerli $v0, 1syscallla $a0, whitespaceli $v0, 4syscalladdu $t2, $t2, 1 # $t2 plus oneb out_loopsearch:la $a0, newline # change a new lineli $v0, 4syscallla $a0, search_msg # print the input message for userli $v0, 4syscallli $v0, 5 # read an integer from usersyscallli $t0, -1beq $v0, $t0, end_program # end input if integer is -1b binary_searchend_program:la $a0, thanksli $v0, 4syscallli $v0, 10syscallquick_sort:bgt $a1, $a0, quick_sort_partition # if end index is larger than start index, do partition jr $ra # just return without doing nothingquick_sort_partition:subu $sp, $sp, 32 # frame size = 32sw $fp, 28($sp) # preserve the Frame Pointersw $ra, 24($sp) # preserve the Return Addressaddu $fp, $sp, 32 # move Frame Pointer to new basemove $t0, $a0 # put start index in $t0move $t1, $a1 # put end index in $t1la $t2, array # put array's base address in $t2mul $t3, $t0, 4addu $t3, $t3, $t2lw $t3, ($t3) # find the integer whose index is $t0mul $t4, $t1, 4addu $t4, $t4, $t2lw $t4, ($t4) # find the integer whose index is $t1li $t6, 2addu $t7, $t3, $t4divu $t5, $t7, $t6 # calculate the middle value: ( first + last ) / 2 , store in $t5move $s0, $t0move $s1, $t1 # backup $t0 and $t1sort_loop:left_half:bge $s0, $t1, right_half # if left pointer is larger than or equal to end index, jump outmul $s2, $s0, 4addu $s2, $s2, $t2 # the integer's address is in $s2lw $s3, ($s2) # get the integer whose index is $s0, put it in $s3bge $s3, $t5, right_half # if current integer is bigger than middle value, do right half addu $s0, $s0, 1 # $s0 plus oneb left_halfright_half:ble $s1, $t0, do_swap # if right pointer is smaller than or equal to start index, jump out of loopmul $s4, $s1, 4addu $s4, $s4, $t2 # the integer's address is in $s4lw $s5, ($s4) # get the integer whose index is $s1, put it in $s5ble $s5, $t5, do_swap # if the current integer is smaller than middle value, do swap subu $s1, $s1, 1 # $s1 minus oneb right_halfdo_swap:bgt $s0, $s1, partition # if left pointer is bigger than right pointer, do partitionmul $s2, $s0, 4addu $s2, $s2, $t2 # the integer's address is in $s2lw $s3, ($s2) # get the integer whose index is $s0, put it in $s3mul $s4, $s1, 4addu $s4, $s4, $t2 # the integer's address is in $s4lw $s5, ($s4) # get the integer whose index is $s1, put it in $s5sw $s3, ($s4)sw $s5, ($s2) # swap the two integers in memorybeq $s0, $t1, sort_loopaddu $s0, $s0, 1 # $s0 plus onebeq $s1, $t0, sort_loopsubu $s1, $s1, 1 # $s1 minus oneb sort_loop # go on sorting the integers partition:sw $s0, 20($sp) # preserve $s0 and $t1sw $t1, 16($sp)move $a0, $t0 # sort the left half of the arraymove $a1, $s1 # start from $t0 and end at $s1jal quick_sortlw $s0, 20($sp)lw $t1, 16($sp)move $a0, $s0 # sort the right half of the arraymove $a1, $t1 # start from $s0 and end at $t1jal quick_sortreturn:lw $ra, 24($sp) # restore Return Addresslw $fp, 28($sp) # restore Frame Pointeraddu $sp, $sp, 32 # restore Stack Pointerjr $ra # returnbinary_search:move $t0, $v0 # get the number from usermove $t1, $zero # $t1 is left indexlw $t2, num_of_integersubu $t2, $t2, 1 # $t2 is right indexla $t7, array # put array's base address in $t7 search_loop:subu $t3, $t2, $t1li $t4, 1beq $t3, $t4, judgement # if right - left = 1, compare the integer with both of themaddu $t3, $t1, $t2li $t4, 2div $t3, $t3, $t4 # middle index is ( left + right ) / 2, put in $t3move $t4, $t3mul $t4, $t4, 4addu $t4, $t4, $t7 # the integer's address is in $t4lw $t4, ($t4) # get the integer whose index is $t4, and still put it in $t4 blt $t0, $t4, front_half # if $t0 < $t4, find it in the front halfbgt $t0, $t4, back_half # if $t0 > $t4, find it in the back halfmove $a0, $t3 # if $t0 = $t4, the integer is foundjal get_resultjudgement:move $t5, $t1mul $t5, $t5, 4addu $t5, $t5, $t7lw $t6, ($t5) # find the integer with index of $t1, and put it in $t6move $a0, $t1beq $t0, $t6, jump_to_resultmove $t5, $t2mul $t5, $t5, 4addu $t5, $t5, $t7lw $t6, ($t5) # find the integer with index of $t2, and put it in $t6move $a0, $t2beq $t0, $t6, jump_to_resultla $a0, not_foundli $v0, 4syscallb searchjump_to_result:jal get_resultfront_half:move $t2, $t3 # move the right pointer to the middleb search_loopback_half:move $t1, $t3 # move the left pointer to the middleb search_loopget_result:move $t0, $a0addu $t0, $t0, 1la $a0, posli $v0, 4syscallmove $a0, $t0li $v0, 1syscallb searchReferences①Pick from /wiki/Quicksort, 2009。

MIPS逆向(三)--汇编指令集

MIPS逆向(三)--汇编指令集

655556 op rs rtrd shamtfunct 65516op rs rt⽴即数操作626op跳转地址MIPS逆向(三)--汇编指令集MIPS有三种指令格式:R型功能:寄存器-寄存器ALU操作(算术运算,逻辑运算)I型功能:条件分⽀,跳转J型功能:跳转MIPS常⽤指令集lb / lh / lw :从存储器中读取⼀个byte/half word/word的数据到寄存器中.sb/sh/sw:把⼀个byte/half word/word的数据从寄存器存储到存储器中mov/movz/movn: 复制,n为负,z为零。

mov $1,$2; movz $1,$2,$3($3为零则复制$2到$1)。

trap: 根据地址向量转⼊管态。

eret: 从异常中返回到⽤户态。

算术类:add/addu: 把两个定点寄存器的内容相加;u为不带符号加,如 rd = rs + rtaddi/addiu: 把⼀个寄存器的内容加上⼀个⽴即数;u为不带符号加。

rd = rs + imsub/subu:把两个定点寄存器的内容相减。

rd = rs - rtdiv/divu: 两个定点寄存器的内容相除。

mul/mulu: 两个定点寄存器的内容相乘。

slt/slti/sltui:如果rs的值⼩于rt,那么设置rd的值为1,否则设置rd的值为0。

rd = (rs < rt) ? 1 : 0 ; rd = (rs < im) ? 1 : 0逻辑类:and/andi:与运算,两个寄存器中的内容相与 ;i为⽴即数。

rd = rs & rt ; rd = rs & imor/ori: 或运算。

rd = rs | rt ; rd = rs | imxor/xori:异或运算。

rd = rs ^ rt ;rd = rs ^ imnor/nori: 取反运算。

rd = !(rs | rt)跳转类:j/jr/jal/jalr: j直接跳转 PC = { (PC+4) [31,28] , addr, 00};jr使⽤寄存器跳转 PC = rs; jal $31 = PC;PC = {(PC+4)[31,28],addr,00}beq/beqz/benz/bne: 条件转移eq相等,z零,ne不等。

微处理器结构04MIPS ISA与汇编程序实例

微处理器结构04MIPS ISA与汇编程序实例

MIPS
Workstations, Embedded, supercomputers
Licensable architecture, Synthesizeable cores for embedded markets
Cisco, Linksys and Mikrotik's routerboard routers, cable modems and ADSL modems, smartcards, laser printer, set-top boxes, robots, handheld computers, Sony PlayStation 2 and Sony PlayStation Portable, cellphone/PDA applications
Meaning $s1 = $s2 + $s3 $s1 = $s2 - $s3 $s1 = Mem[$s2 + 100] Mem[$s2 + 100] = $s1
Loongson
MIPS寄存器
Register 0 31 Register 1 31

10

10
……
32 GPRs
Register 31 31

10
Design Principle 2: Smaller is faster.
MIPS寄存器命名
No. Name
Usage
No.
0 $zero Constant 0
Meaning
add $s1, $s2, $s3 $s1 = $s2 + $s3
sub $s1, $s2, $s3 $s1 = $s2 - $s3
lw $s1, 100($s2) $s1 = Mem[$s2 + 100]

mips机器码汇编对照表

mips机器码汇编对照表

mips机器码汇编对照表助记符说明字节周期代码MOV A,Rn 寄存器送A 1 1 E8--EFMOV A,data 直接字节送A 2 1 E5MOV A,@Ri 间接RAM 送A 1 1 E6--E7MOV A,#data 立接数送A 2 1 74MOV Rn,A A 送寄存器 1 1 F8--FFMOV Rn,data 直接数送寄存器 2 2 A8--AFMOV Rn,#data 立即数送寄存器 2 1 78--7FMOV data,A A 送直接字节 2 1 F5MOV data,Rn 寄存器送直接字节 2 1 88—8FMOV data,data 直接字节送直接字节 3 2 85MOV data,@Ri 间接Rn 送直接字节 2 2 86;87MOV data,#data 立即数送直接字节 3 2 75MOV @Ri,A A 送间接Rn 1 2 F6;F7MOV @Ri,data 直接字节送间接Rn 1 1 A6;A7MOV @Ri,#data 立即数送间接Rn 2 2 76;77MOV DPTR,#data16 16 位常数送数据指针 3 1 90 MOV C,bit 直接位送进位位 2 1 A2MOV bit,C 进位位送直接位 2 2 92MOVC A,@A+DPTR A+DPTR 寻址程序存贮字节送A 3 2 93 MOVC A,@A+PC A+PC 寻址程序存贮字节送A 1 2 83 MOVX A,@Ri 外部数据送A(8 位地址) 1 2 E2;E3 MOVX A,@DPTR 外部数据送A(16 位地址) 1 2 E0 MOVX @Ri,A A 送外部数据(8 位地址) 1 2 F2;F3 MOVX @DPTR,A A 送外部数据(16 位地址) 1 2 F0 PUSH data 直接字节进栈道,SP 加1 2 2 C0POP data 直接字节出栈,SP 减1 2 2 D0XCH A,Rn 寄存器与A 交换 1 1 C8—CFXCH A,data 直接字节与A 交换 2 1 C5XCH A,@Ri 间接Rn 与A 交换 1 1 C6;C7XCHD A,@Ri 间接Rn 与A 低半字节交换 1 1 D6;D72.逻辑运算指令(35条)助记符说明字节周期代码ANL A,Rn 寄存器与到A 1 1 58—5FANL A,data 直接字节与到A 2 1 55ANL A,@Ri 间接RAM与到A 1 1 56;57ANL A,#data 立即数与到A 2 1 54ANL data,A A与到直接字节 2 1 52ANL data,#data 立即数与到直接字节 3 2 53 ANL C,bit 直接位与到进位位 2 2 82ANL C,/bit 直接位的反码与到进位位 2 2 B0 ORL A,Rn 寄存器或到A 1 1 48—4FORL A,data 直接字节或到A 2 1 45ORL A,@Ri 间接RAM或到A 1 1 46;47ORL A,#data 立即数或到A 2 1 44ORL data,A A或到直接字节 2 1 42ORL data,#data 立即数或到直接字节 3 2 43 ORL C,bit 直接位或到进位位 2 2 72ORL C,/bit 直接位的反码或到进位位 2 2 A0 XRL A,Rn 寄存器异或到A 1 1 68—6FXRL A,data 直接字节异或到A 2 1 65XRL A,@Ri 间接RAM异或到A 1 1 66;67XRL A,#data 立即数异或到A 2 1 64XRL data,A A异或到直接字节 2 1 62XRL data,#data 立即数异或到直接字节 3 2 63 SETB C 进位位置1 1 1 D3SETB bit 直接位置1 2 1 D2CLR A A清0 1 1 E4CLR C 进位位清0 1 1 C3CLR bit 直接位清0 2 1 C2CPL A A求反码 1 1 F4CPL C 进位位取反 1 1 B3CPL bit 直接位取反 2 1 B2RL A A循环左移一位 1 1 23RLC A A 带进位左移一位 1 1 33RR A A右移一位 1 1 03RRC A A 带进位右移一位 1 1 13SWAP A A 半字节交换 1 1 C43.算术运算指令(24条)助记符说明字节周期代码ADD A,Rn 寄存器加到A 1 1 28—2FADD A,data 直接字节加到A 2 1 25ADD A,@Ri 间接RAM 加到A 1 1 26;27 ADD A,#data 立即数加到A 2 1 24ADDC A,Rn 寄存器带进位加到A 1 1 38—3F ADDC A,data 直接字节带进位加到A 2 1 35ADDC A,@Ri 间接RAM 带进位加到A 1 1 36;37 ADDC A,#data 立即数带进位加到A 2 1 34SUBB A,Rn 从A 中减去寄存器和进位 1 1 98—9F SUBB A,data 从A 中减去直接字节和进位 2 1 95 SUBB A,@Ri 从A 中减去间接RAM 和进位 1 1 96;97 SUBB A,#data 从A 中减去立即数和进位 2 1 94 INC A A加1 1 1 04INC Rn 寄存器加1 1 1 08—0FINC data 直接字节加1 2 1 05INC @Ri 间接RAM 加1 1 1 06;07INC DPTR 数据指针加1 1 2 A3DEC A A减1 1 1 14DEC Rn 寄存器减1 1 1 18—1FDEC data 直接字节减1 2 1 15DEC @Ri 间接RAM 减1 1 1 16;17MUL AB A乘B 1 4 A4DIV AB A被B除 1 4 84DA A A十进制调整 1 1 D44.转移指令(22条)助记符说明字节周期代码AJMP addr 11 绝对转移 2 2 *1LJMP addr 16 长转移 3 2 02SJMP rel 短转移 2 2 80JMP @A+DPTR 相对于DPTR 间接转移 1 2 73JZ rel 若A=0 则转移 2 2 60JNZ rel 若A≠0 则转移 2 2 70JC rel 若C=1 则转移 2 2 40JNC rel 若C≠1 则转移 2 2 50JB bit,rel 若直接位=1 则转移 3 2 20JNB bit,rel 若直接位=0 则转移 3 2 30JBC bit,rel 若直接位=1 则转移且清除 3 2 10CJNE A,data,rel 直接数与A 比较,不等转移 3 2 B5CJNE A,#data,rel 立即数与A 比较,不等转移 3 2 B4CJNE @Ri,#data,rel 立即数与间接RAM比较,不等转移 3 2 B6;B7CJNE Rn,#data,rel 立即数与寄存器比较不等转移 3 2 B8—BFDJNZ Rn,rel 寄存器减1 不为0 转移 2 2 D8—DFDJNZ data,rel 直接字节减1 不为0 转移 3 2 D5ACALL addr 11 绝对子程序调用 2 2 *1 LCALL addr 16 子程序调用 3 2 12 RET 子程序调用返回 1 2 22RETI 中断程序调用返回 1 2 32NOP 空操作 1 1 00。

八皇后问题MIPS汇编实现

八皇后问题MIPS汇编实现

八皇后问题一、计算的规则和任务国际象棋中的皇后可以吃掉与它在同一行、同一列、同一对角线上的棋子。

八皇后问题,即在8×8的国际象棋棋盘上放置8个皇后,要求任意两个皇后不能在同一行、同一列或同一条对角线上。

求出如此放置方法的种数。

一种解决问题的思路是一行放置皇后,如果当前放置的皇后与前面的皇后不存在冲突时,则继续摆下一个皇后,否则跳到上一个皇后,重新摆置。

二、八皇后问题的C实现:递归放置第个皇后:判断放置第个皇后时是否无冲突:判断第个皇后放上去之后,是否合法,即是否无冲突。

三、相应的MIPS实现.datastr:.asciiz"Eight Queen problems, the number of methods of setting eight queens:".text#main中变量与保存寄存器和临时储存器的对应:#输入n(=8):寄存器$s1#输入iCount:寄存器$s2#函数Queen(n,QUEEENS,iCount)的返回值:寄存器$v0#函数Queen(n,QUEENS,iCount)中,n:$a0, QUEENS:$a1, iCount:$a2#函数Valid(n)的返回值:寄存器$v1#--------------------------------main function--------------------------------# main:addi $sp,$sp,-32 #利用栈开辟八位数组,site[8]addi $s5,$sp,0 #$s5指向数组的基址site[0]addi $s2,$zero,0 #iCount=0li $v0,4 #执行syscall中的print_int功能la $a0,str #printf("Eight Queen problems, the number of methods of setting eight queens:")syscalladdi $v0,$zero,8add $s1,$zero,$v0 #$v0内的值存入n中addi $a0,$zero,0 #$a0=0add $a1,$0,$s1 #$a1=nadd $a2,$0,$s2 #$a2=iCountjal Queen #调用Queen函数move $a0,$v0 #函数Queen返回值存入$a0li $v0,1 #执行syscall中的输出整型数的功能,输出最终结果syscallli $v0,10 #结束程序运行syscall#Queen函数变量与保存寄存器和临时寄存器对应:#过程调用中的栈指针:$sp#Queen函数返回的地址:$ra#Queen函数内,循环中的i对应的寄存器:$s4#Queen函数中site[n]的地址对应的寄存器:$s6#--------------------------------Queen function--------------------------------#Queen:addi $sp,$sp,-24 #开辟栈空间sw $ra,20($sp) #$ra入栈sw $v0,16($sp) #$v0入栈sw $a0,12($sp) #$a0入栈sw $a2,8($sp) #$a2入栈sw $s6,4($sp) #$s6入栈bne $a0,$a1,exit #n不等于QUEENS时exit;n等于QUEENS,执行下一条指令 addi $a2,$a2,1 #iCount=iCount+1sw $a2,8($sp) #更新iCount值,存入栈j EXIT2 #直接跳到EXIT2exit:addi $s4,$zero,1 #i=1sw $s4,0($sp) #i值入栈loop1:lw $a0,12($sp) #读栈中n值,存入$a0sle $t0,$s4,$a1 #i<=QUEENS时,$t0=1beq $t0,$zero,EXIT2 #$t0=0跳转到EXIT2,否则执行下一指令sll $t1,$a0,2 #将n左移两位,存入临时寄存器$t0,即$t0=n*4add $s6,$t1,$s5 #数组site[n]的基址增加4*nsw $s4,0($s6) #site[n]=ijal Valid #调用函数Valid(n),返回值为$v1beq $v1,$0,EXIT1 #$v1=0时跳转到EXIT1,否则执行下一指令addi $a0,$a0,1 #n=(n+1)jal Queen #递归调用函数QUEEN(n+1,QUEENS,iCount)add $a2,$zero,$v0 #iCount=Queen(n+1,QUEENS,iCount)sw $a2,8($sp) #更新当前栈中iCountEXIT1:lw $s4,0($sp) #读取之前保存的i值存入$s4中addi $s4,$s4,1 #i=(i+1)sw $s4,0($sp) #更新当前栈中i值j loop1 #跳转回loop1EXIT2:lw $s4,0($sp) #$s4出栈lw $s6,4($sp) #$s6出栈lw $a2,8($sp) #a2出栈lw $a0,12($sp) #$a0出栈lw $ra,20($sp) #$ra出栈addi $sp,$sp,24 #释放栈空间move $v0,$a2 #将返回值iCount存入$v0jr $ra #根据跳转寄存器中的返回地址跳转#Valid函数变量与保存寄存器和临时寄存器对应:#i:$t4#site[n]的地址:$s6#site[i]的地址:$t3#site[n]的值:$t6中#site[i]的值:$t7中#--------------------------------Valid function--------------------------------#Valid:addi $t4,$zero,0 #i=0loop2:slt $t0,$t4,$a0 #i<n时令t0=1beq $t0,$zero,exit1 #$t0=0时跳转到exit1,否则执行下一指令sll $t1,$a0,2 #n左移两位,存入临时寄存器$t1,即$t1=n*4sll $t2,$t4,2 #i左移两位,存入临时寄存器$t2,即$t2=i*4add $s6,$s5,$t1 #数组site[n]的基址增加4*nadd $t3,$s5,$t2 #数组site[i]的基址增加4*ilw $t6,0($s6) #$t6=site[n]lw $t7,0($t3) #$t7=site[i]sub $t0,$t7,$t6 #$t0=(site[i]-site[n])abs $t0,$t0 #$t0=(abs(site[i]-site[n]))sub $t1,$a0,$t4 #$t1=(n-i)seq $t2,$t0,$t1 #abs(site[i]-site[n])=(n-i)时,$t2=1beq $t6,$t7,exit0 #site[n]=site[i]时跳转到exit0,否则执行下一指令bne $t2,$zero,exit0 #$t2不等于0时跳转到exit0,否则执行下一指令addi $t4,$t4,1 #i=(i+1)j loop2 #跳回loop2exit1:addi $v1,$zero,1 #$v1=1,即Valid返回值赋为1jr $raexit0:addi $v1,$zero,0 #$v1=0,即Valid返回值赋为0jr $ra四、程序运行结果五、设计原理与思路八皇后算法采用回溯法。

使用MIPS汇编语言编程

使用MIPS汇编语言编程

If/Else 语句
C 代码
if (i == j) f = g + h;
else f = f – i;
MIPS 汇编代码
# $s0=f, $s1=g, $s2=h, $s3=i, $s4=j
bne $s3, $s4, L1 add $s0, $s1, $s2 j done L1: sub $s0, $s0, $s3 done:
While 循环
C 代码
// 2x = 128,求x
int pow = 1; int x = 0;
while (pow != 128) { pow = pow * 2; x = x + 1;
}
MIPS 汇编代码
# $s0 = pow, $s1 = x
addi $s0, $0, 1 add $s1, $0, $0 addi $t0, $0, 128 while: beq $s0, $t0, done sll $s0, $s0, 1 addi $s1, $s1, 1 j while done:
数组
• 5个元素的数组 • 基地址
• Base address = 0x12348000 • 第1个元素的地址, array[0]
0x12340010 0x1234800C 0x12348008 0x12348004 0x12348000
array[4] array[3] array[2] array[1] array[0]
使用MIPS 汇编语言编程
编程
• 高级语言:
– 如, C, Java, Python – 在抽象层的更高级别
• 常见高级软件结构:
– if/else 语句 – for 循环 – while 循环 – 数据索引 – function 调用

MIPS编程实例集锦(2)

MIPS编程实例集锦(2)
{ int f;
f = (g + h) - (i + j);
return f; }
Write program for switch case :
switch (k) {
case 0: f = i + j; break;
case 1: f = g + h; break;
case 2: f = g - h; break;
# All done, thank you!
li $v0,10 # code for exit
syscall # exit program
Output:
2.Write a program to display given integer to be reverse ?
Code:
.data
mess1:.asciiz "Enter string: "
# print out text for the result
li $v0,4 #code for print_string
la $a0,resultInt # point $a0 to result string
syscall # print the result string
# print out the result
li $v0,5 # code for read_int
syscall #get int from user --> returned in $v0
move $t0,$v0 # move the resulting int to $t0
# compute the next integer
addi $t0, $t0, 1 # t0 <-- t0 + 1

MIPS指令集及汇编

MIPS指令集及汇编
z 1986年推出R2000处理器, z 1988年推R3000处理器, z 1991年推出第一款64位商用微处器R4000。 z 之后又陆续推出R8000(1994年)、R10000(1996年)
和R12000(1997年)等型号。 z 随后,MIPS公司把重点放在嵌入式系统。
{ 1999年发布MIPS32和MIPS64架构标准,集成了所有原 来MIPS指令集,并且增加了许多更强大的功能。陆续开发 了高性能、低功耗的32位处理器内核(core)MIPS324Kc 与高性能64位处理器内核MIPS64 5Kc。
{ 应用广泛的32位MIPS CPU包括R2000,R3000 其ISA都是MIPS I,另一个广泛使用的、含有许多 重要改进的64位MIPS CPU R4000及其后续产 品,其ISA版本为MIPS III
一、 MIPS简介
{ 1984年,MIPS计算机公司成立。1992年,SGI收购了 MIPS计算机公司。1998年,MIPS脱离SGI,成为MIPS 技术公司。
龙芯2E笔记本电脑运行 OpenOffice打开Word文档
ቤተ መጻሕፍቲ ባይዱ
二、MIPS体系结构
{ 指令集体系结构类型:寄存器——寄存器型
(1)寄存器的特点 (2)整数乘法单元和寄存器 (3)寻址方式 (4)存储器和寄存器中的数据类型 (5)流水线冒险
二、 MIPS体系结构——(1)寄存器
{ MIPS 包含32个通用寄存器
{ 2000年,MIPS公司发布了针对MIPS32 4Kc的版本以及 64位MIPS 64 20Kc处理器内核。
一、 MIPS简介
{ 基于龙芯2E处理器的千元的PC、1999元的笔记本电脑、 意法半导体3000万元购买龙芯2E 5年的生产和销售权, 国产操作系统内核在龙芯2E上测试成功。

mips汇编语言代码示例

mips汇编语言代码示例

mips汇编语言代码示例MIPS汇编语言代码示例:计算两个数的和MIPS汇编语言是一种基于RISC(精简指令集计算机)架构的汇编语言,它被广泛应用于嵌入式系统、数字信号处理、计算机视觉等领域。

本文将以计算两个数的和为例,介绍MIPS汇编语言的基本语法和指令。

我们需要了解MIPS汇编语言的寄存器。

MIPS架构中有32个通用寄存器,分别用$0~$31表示。

其中,$0$寄存器始终为$0$,$1~$3寄存器用于函数调用,$4~$7寄存器用于保存函数调用时的参数,$8~$15寄存器用于保存临时变量,$16~$23寄存器用于保存全局变量,$24~$25寄存器用于保存函数调用时的返回值,$26~$27寄存器用于保存系统调用的参数,$28寄存器用于保存全局指针,$29寄存器用于保存栈指针,$30寄存器用于保存帧指针,$31寄存器用于保存程序计数器。

接下来,我们可以编写计算两个数的和的MIPS汇编语言代码。

假设我们要计算$a+b$的值,可以使用以下代码:```.dataa: .word 2b: .word 3sum: .word 0.text.globl mainmain:lw $t0, alw $t1, badd $t2, $t0, $t1sw $t2, sumli $v0, 10syscall```我们使用.data段定义了三个变量:$a$、$b$和$sum$。

$a$和$b$分别被初始化为$2$和$3$,$sum$被初始化为$0$。

接着,我们使用.text段定义了一个全局函数$main$,并使用.globl指令将其声明为全局函数。

在$main$函数中,我们首先使用lw指令将$a$和$b$的值分别加载到$t0$和$t1$寄存器中。

然后,我们使用add指令将$t0$和$t1$的值相加,并将结果保存到$t2$寄存器中。

最后,我们使用sw指令将$t2$的值存储到$sum$变量中。

最后,我们使用li指令将$v0$寄存器设置为$10$,并使用syscall指令退出程序。

MIPS GCC 嵌入式汇编(龙芯适用)

MIPS GCC 嵌入式汇编(龙芯适用)
"assembly code"
: output_operand
: input_operand
: clobbered_operand ); 以一个例子来说明: 如果我们要读取 CP0 25 号硬件计数寄存器的值,并返回之,可以这样:
int get_counter()
{ int rst;
asm(
"mfc0 %0, $25\t\n"
当前版本: 0.3 1. GCC 内嵌汇编的基本格式
asm("assembly code"); 如: asm("syscall"); //触发一个系统调用 如果有多条指令,则需在指令尾部添加'\t'和'\n',如:
asm("li v0, 4011\t\n" "syscall"); 括号里的字符串 GCC 前端不作分析,直接传给汇编器 as ,故而相联指令间需 插入换行符。 '\t' 加入只为排版对齐一些而已,可以使用 gcc -S tst.c -o tst.s 查看生成的 tst.s 因为 GCC 并不对 asm 后括号中的指令作分析,故而如果指令修改一些的寄存 器的值,GCC 是 不知道的,这个会引入一些问题。 另外 asm 可以替换为 __asm__ ,效果等价。__asm__ 一般用于头文件中,防 止关键字 asm 可能与一些变量、函数名冲突。 内嵌汇编如何与 C 变量交换数据? 2. GCC 内嵌汇编扩展格式 asm (
constant which is not `I', `K', or `L') `N' Negative 16-bit constant `O' Exact power of two `P' Positive 16-bit constant `G' Floating point zero `Q' Memory reference that can be loaded with more than one instruction (`m' is preferable for `asm' statements) `R' Memory reference that can be loaded with one instruction (`m' is preferable for `asm' statements) `S' Memory reference in external OSF/rose PIC format (`m' is preferable for `asm' statements) 5. 32 位下传递 64 位数据 A. 读取: long long counter; asm( ".set mips3\n\t" "dmfc0 %M0, $25\n\t" "dsll %L0, %M0, 32\n\t" "dsrl %M0, %M0, 32\n\t" "dsrl %L0, %L0, 32\n\t" ".set mips0\n\t" : "=r" (counter) ); B. 写入 long long counter = 0x0000001000000100; asm( ".set mips3\n\t" "dsll %L0, %L0, 32\n\t" "dsrl %L0, %L0, 32\n\t" "dsll %M0, %M0, 32\n\t" "or %L0, %L0, %M0\n\t" "dmtc0 %L0, $25\n\t" ".set mips0\n\t" : "=r" (counter) );

MIPS汇编

MIPS汇编

溢出(有符号数)
两个正数相加,如果结果为负数,就产生了溢出。 两个负数相加,如果结果为正数,就产生了溢出。 换个角度来说: 正数 - 负数 = 负数,就产生了溢出。 负数 - 正数 = 正数,就产生了溢出。
无符号数会出错的地方: 1、两个数相加超出了数所能表示的大小,产生进位 2、小整数减大整数,会借位
or d,s,t d=s | t ori d,s,j d=s|(unsigned)j,跟一个常数执行“或”操作OR sllv d,t,s d=t<<(s%32) (shift left logic by variable) sll d,s,shf d=s<<shf;sll d,t,s,d=t<<(s%32) sra d,s,shf d=(signed)s>>shf,shift-right arithmetic,算术右移, 最高位填充,适用于有符号数,sra d,s,t,d=(signed)s>>(t%32) srav d,s,t sra d,s,t srl d,s,shf d=(unsigned)s>>shf ,shift-right logical,逻辑右移,类 似C的无符号量的移位 srl d,s,t d=((unsigned)s>>(t%32) srlv d,s,t srl d,s,t slt d,s,t d=((signed)s<(signed)t)?1:0 (set on less than) sltu d,s,t d=((unsigned)s<(unsigned)t)?1:0 slti d,s,j d=((signed)s<(signed)j)?1:0 sltiu d,s,t d=((unsigned)s<(unsigned)j)?1:0

c语言 mips指令

c语言 mips指令

c语言mips指令MIPS指令集是一种为MIPS架构的微处理器设计的计算机指令集。

这是一种RISC(精简指令集计算机)指令集,它的设计目标是使其在各种不同的硬件实现上都能高效地运行。

对于C语言,通常使用编译器将C代码编译成MIPS汇编指令。

例如,下面是一个简单的C程序,用于计算两个整数之和:c复制代码#include<stdio.h>int main() {int a = 5;int b = 10;int sum = a + b;printf("Sum is: %d\n", sum);return0;}这个程序可以编译成MIPS汇编语言,如下所示:assembly复制代码# MIPS assembly code for the above C program.text.globl mainmain:# Load the values of a and b into $t0 and $t1 respectivelyaddi $t0, $zero, 5 # Load the value of a into $t0addi $t1, $zero, 10 # Load the value of b into $t1add $t2, $t0, $t1 # Add the values in $t0 and $t1 and store the result in $t2 (sum)li $v0, 1 # Load the system call number for print_int into $v0 move $a0, $t2 # Move the value in $t2 (sum) into $a0, which is the first argument of print_intsyscall # Call the kernel to execute print_intli $v0, 10 # Load the system call number for exit into $v0syscall # Call the kernel to exit以上MIPS汇编代码对应于C程序的各个部分。

MIPS编程实例集锦(1)

MIPS编程实例集锦(1)

PROBLEMS IN MIPS PROGRAMMING∙How to write a program to add all positive numbers once & all negative numbers once?∙Write a program to display the prime numbers between 1to 1000?∙Program to write Fibonacci using recursion?∙Write a program to swap the 2 strings?∙Write a program to reverse of given string?∙Write a program to multiply 2 numbers ( eg 128*256) ?∙Write a program to convert given number into other base ? ( eg: given number in decimal converting to base like 2 it is binary so itconverts decimal to binary )∙Write a program to convert “COMPUTER ORGANISATION”to lower case?∙Write program to bubble sort of the given numbers?∙Write a program to comparing strings whether or not ?∙Write a program convert lowercase letters to uppercase letters?∙Write a program for multiplication table?∙Write a program to find given number is even or odd?∙Write a program for palindrome number?∙Writing program for raising power?∙Write a program for sum of 3 integers?1)How to write a program to add all positive numbers once & allnegative numbers once?Code :.dataarray: .word -4, 5, 8, -1msg1: .asciiz "\n The sum of the positive values = " msg2: .asciiz "\n The sum of the negative values = " .globl main.textmain:li $v0, 4 # system call code for print_strla $a0, msg1 # load address of msg1. into $a0 syscall # print the stringla $a0, array # Initialize address Parameterli $a1, 4 # Initialize length Parameterjal sum # Call summove $a0, $v0 # move value to be printed to $a0li $v0, 1 # system call code for print_intsyscall # print sum of Pos:li $v0, 4 # system call code for print_strla $a0, msg2 # load address of msg2. into $a0 syscall # print the stringli $v0, 1 # system call code for print_intmove $a0, $v1 # move value to be printed to $a0 syscall # print sum of negli $v0, 10 # terminate program run andsyscall # return control to systemsum:li $v0, 0li $v1, 0 # Initialize v0 and v1 to zeroloop:blez $a1, retzz # If (a1 <= 0) Branch to Returnaddi $a1, $a1, -1 # Decrement loop countlw $t0, 0($a0) # Get a value from the arrayaddi $a0, $a0, 4 # Increment array pointer to next word bltz $t0, negg # If value is negative Branch to negg add $v0, $v0, $t0 # Add to the positive sumb loop # Branch around the next two instructions negg:add $v1, $v1, $t0 # Add to the negative sumb loop # Branch to loopretzz:jr $ra # ReturnOutput:2)Write a Program factorial of a given number?.textfact: # ALLOCATE A FRAME FOR THE DYNAMIC LINK, RETURN ADDRESS,AND STATIC LINK (TOTAL: 3*4 = 12 BYTES)sw $fp, ($sp) # PUSH OLD FRAME POINTER (DYNAMIC LINK)move $fp, $sp# FRAME POINTER NOW POINTS TO THE TOP OF STACKsubu $sp, $sp, 12 # ALLOCATE TWELVE BYTES IN THE STACK SAVE RETURN ADDRESS IN FRAMEsw $ra, -4($fp) # SAVE STATIC LINK IN FRAMEsw $v0, -8($fp)# IF n>0 GOTO recurs (THE ARGUMENT n IS LOCATED AT 4($fp))lw $a0, 4($fp)bgt $a0, 1, recurs # OTHERWISE RETURN 1li $v0, 1b returnrecurs: # PUSH n-1 AS THE ARGUMENT TO factlw $a0, 4($fp)subu $a0, $a0, 1sw $a0, ($sp)subu $sp, $sp, 4# LOAD STATIC LINKlw $v0, -8($fp)jal fact # CALL fact# MULTIPLY THE RESULT OF fact(n-1) BY nmul $v0, $v0, $a0return: # RETURN FROM fact (RESTORE REGISTERS AND POP FRAME)lw $ra, -4($fp)move $sp, $fplw $fp, ($sp) # RESTORE OLD FRAME POINTERjr $ra.text# main MUST BE GLOBAL.globl mainmain: # ALLOCATE A FRAME FOR THE DYNAMIC LINK, RETURN ADDRESS, STATIC LINK, AND FOR THE INTEGERS i AND res (TOTAL: 5*4 = 20 BYTES)sw $fp, ($sp) # PUSH OLD FRAME POINTER (DYNAMIC LINK)move $fp, $sp# FRAME POINTER NOW POINTS TO THE TOP OF STACKsubu $sp, $sp, 20 # ALLOCATE TWENTY BYTES IN THE STACK# SAVE RETURN ADDRESS IN FRAMEsw $ra, -4($fp)# SAVE STATIC LINK IN FRAMEsw $v0, -8($fp)loop: # PRINT prompt.dataprompt: .asciiz "Enter a Number? ".textli $v0, 4syscallli $v0, 5syscallsw $v0, -12($fp)# IF i<=0 GOTO EXITblez $v0, exitlw $a0, -12($fp) # PUSH isw $a0, ($sp)subu $sp, $sp, 4move $v0, $fp# THE STATIC LINK OF fact IS THE FRAME POINTER OF main # CALL factjal fact# STORE THE RESULT OF fact(i) INTO res (res IS LOCATED AT -16($fp)) sw $v0, -16($fp) # PRINT answer.dataanswer: .asciiz "The factorial is: ".textli $v0, 4la $a0, answersyscallli $v0, 1lw $a0, -16($fp)syscall# PRINT END OF LINE.dataendl: .asciiz "\n".textli $v0, 4la $a0, endlsyscallb loop # LOOP BACKexit: # RETURN FROM main (RESTORE REGISTERS AND POP FRAME) lw $ra, -4($fp)move $sp, $fplw $fp, ($sp) # RESTORE OLD FRAME POINTERjr $raOutput:3)Write a program to display the prime numbers between 1to 1000?.datainput_int: .asciiz "The prime numbers between 1 and 1000 are:\n"newline: .asciiz "\n"space: .asciiz " ".text.align 2.globl mainmain:la $a0, input_int #print "The prime numbers between 1 and 1000 are:"li $v0,4syscallli $a1,2 #the first prime is 2li $s2,1000 #Stop searching for primes after 1000li $t5,0 #zero initializedli $t2,6 #initialized to 6li $s0,1jal primeloop #jumps backprimeloop:#Find primesbeq $s2,0,exitsub $s2,$s2,1 #Subtract to keep track as in counter#Set $t1 to 2li $t1,2divide:div $a1,$t1 #Divides by 2 to get next primemflo $t3slt $v0,$t3,$t1 #if quotient less than divisor stopbeq $v0,$s0,fdprime #determine if prime ($v0=1), if prime prints #If remainder is zero, it is a composite,not primemfhi $t4beq $t4,0,nprime#Try next divisoradd $t1,$t1,1j dividefdprime:li $v0,1move $a0,$a1syscallli $v0,4la $a0,spacesyscalladdi $t5,$t5,1beq $t5,$t2,skipnprime:#Advances to the next numberadd $a1,$a1,1jr $ra #goes back to find the primesskip:li $v0,4la $a0,newlinesyscallli $t5,0j nprimeexit:Output :4)Program to write Fibonacci using recursion?.datamsg1:.asciiz "Give a number: ".text.globl mainmain:li $v0,4la $a0,msg1syscall #print msgli $v0,5syscall #read an intadd $a0,$v0,$zero #move to $a0jal fib #call fibadd $a0,$v0,$zeroli $v0,1syscallli $v0,10syscallfib:#a0=y#if (y==0) return 0;#if (y==1) return 1;#return( fib(y-1)+fib(y-2) );addi $sp,$sp,-12 #save in stacksw $ra,0($sp)sw $s0,4($sp)sw $s1,8($sp)add $s0,$a0,$zeroaddi $t1,$zero,1beq $s0,$zero,return0beq $s0,$t1,return1addi $a0,$s0,-1jal fibadd $s1,$zero,$v0 #s1=fib(y-1)addi $a0,$s0,-2jal fib #v0=fib(n-2)add $v0,$v0,$s1 #v0=fib(n-2)+$s1 exitfib:lw $ra,0($sp) #read registers from stack lw $s0,4($sp)lw $s1,8($sp)addi $sp,$sp,12 #bring back stack pointer jr $rareturn1:li $v0,1j exitfibreturn0 : li $v0,0j exitfibOutput:5)Write a program to swap the 2 strings?.globl main.align 2main:la $a0, first_string_locationla $a1, second_string_locationjal strswapla $a0, first_string_locationli $v0,4syscallla $a0, second_string_locationli $v0,4syscallli $v0,10 # Quitsyscallstrswap:addi $sp,$sp,-200 # Prepare space on the stacksw $ra, 4($sp) # Save return registersw $a0, 8($sp) # Save String1sw $a1, 12($sp) # Save String2# Move first string to temporary locationla $a0, 16($sp) # Temp string - destinationlw $a1, 8($sp) # String 1 - sourcejal strcpy# Move second string to first string locationlw $a0, 8($sp) # String 1 - destinationlw $a1, 12($sp) # String 2 - sourcejal strcpy# Move temp string to second string location lw $a0, 12($sp) # String 2 - destinationla $a1, 16($sp) # Temp String - sourcejal strcpylw $ra, 4($sp) # Wrap it upaddi $sp, $sp, 200jr $rastrcpy:lb $t0,($a1)sb $t0,($a0)addi $a1,$a1,1addi $a0,$a0,1bnez $t0,strcpyjr $ra.datafirst_string_location: .asciiz "First string\n" nulls: .space 10second_string_location: .asciiz "Second string\n" nulls2: .space 10Output:6)Write a program to reverse of given string? .data.align 1String: .space 14 #*changemsg1: .asciiz "Pls give a character: "msg2: .asciiz "\n"msg3: .asciiz "String is: "msg4: .asciiz "\nString Reversed is: ".text.globl mainmain:addi $s0,$zero,13 #*changeaddi $t0,$zero,0in:la $a0,msg2li $v0,4syscallli $v0,4la $a0,msg1syscallli $v0,12syscalladd $t1,$v0,$zerosb $t1,String($t0)addi $t0,$t0,1slt $t1,$s0,$t0beq $t1,$zero,insb $zero,String($t0) #ending zerola $a0,msg2li $v0,4syscallla $a0,msg2li $v0,4syscallla $a0,msg3li $v0,4syscallla $a0,Stringli $v0,4syscalladdi $a1,$zero,14 #pass length-*change jal stringreverse #reversela $a0,msg2li $v0,4syscallla $a0,msg4li $v0,4syscallla $a0,Stringli $v0,4syscallli $v0,10syscallstringreverse:add $t0,$a0,$zero #beginning address add $t1,$zero,$zero #i=0addi $t2,$a1,-1 #j=length-1loop:add $t3,$t0,$t1lb $t4,0($t3) #lb String[i]add $t5,$t0,$t2lb $t6,0($t5) #lb String[j]sb $t4,0($t5) #String[j]=String[i]sb $t6,0($t3) #String[i]=String[j] addi $t1,$t1,1 #i++addi $t2,$t2,-1 #j--#if i>=j break - $t1<$t2slt $t6,$t2,$t1beqz $t6,loop jr $ra Output:7)Write a program to multiply 2 numbers ( eg 128*256) ?.datamsg: .asciiz "The product is ".text # this is program code.align 2 # instructions must be on word boundaries.globl main # main is a global labelmain:# multiply 128 * 256addi $a0,$zero,128addi $a1,$zero,256jal multiplymove $s0,$v0li $v0,4la $a0, msgsyscallli $v0,1move $a0,$s0syscallli $v0,10syscallmultiply:sub $sp,$sp,4 # make room for $t0sw $t0,0($sp) # put t0 on the stack# start with $t0 = 0add $t0,$zero,$zeromult_loop:# loop on a1beq $a1,$zero,mult_eoladd $t0,$t0,$a0sub $a1,$a1,1j mult_loopmult_eol:# put the result in $v0add $v0,$t0,$zero# restore $t0lw $t0,0($sp)add $sp,$sp,4jr $raOutput :8)Write a program to convert given number into other base ? ( eg:given number in decimal converting to base like 2 it is binary so it converts decimal to binary ).datamsg1: .asciiz "Please insert value (A > 0) : "msg2: .asciiz "Please insert the number system B you want toconvert to (2<=B<=10): "#Above sting must be in one linemsg3: .asciiz "\nResult : ".text.globl mainmain:addi $s0,$zero,2addi $s1,$zero,10getA:li $v0,4la $a0,msg1syscallli $v0,5syscallblt $v0,$zero,getAmove $t0,$v0 getB:li $v0,4la $a0,msg2 syscallli $v0,5syscallblt $v0,$s0,getB bgt $v0,$s1,getBadd $t1,$zero,$v0li $v0,4la $a0,msg3 syscalladd $a0,$zero,$t0 add $a1,$zero,$t1 jal convertli $v0,10 syscallconvert:#a0=A#a1=Baddi $sp,$sp,-16sw $s3,12($sp) #counter,used to know #how many times we will pop from stack sw $s0,8($sp) #Asw $s1,4($sp) #Bsw $ra,0($sp)add $s0,$zero,$a0add $s1,$zero,$a1beqz $s0,enddiv $t4,$s0,$s1 #t4=A/Brem $t3,$s0,$s1 #t3=A%Badd $sp,$sp,-4sw $t3,0($sp) #save t3add $a0,$zero,$t4 #pass A/Badd $a1,$zero,$s1 #pass Baddi $s3,$s3,1jal convert #call convertend:lw $ra,0($sp)lw $s1,4($sp)lw $s0,8($sp)lw $s3,12($sp)beqz $s3,donelw $a0,16($sp)li $v0,1syscalldone:addi $sp,$sp,20jr $ra #returnOutput:9)Write a program to convert “COMPUTER ORGANISATION”tolower case?Code :.text.globl __startmain: ##execution starts herela $t1, strnextCh: lb $t2, ($t1)beqz $t2, strEndaddi $t4, $zero, 0x61sub $t3, $t2, $t4bgez $t3, l1add $t2, $t2, 32sb $t2, ($t1)l1: add $t1, $t1, 1j nextChstrEnd: la $a0, ansli $v0, 4syscallla $a0, strli $v0,4syscallla $a0, endlli $v0, 4syscallli $v0, 10syscall.datastr: .asciiz "COMPUTER ORGANISATION " ans: .asciiz "lowercase string ="endl: .asciiz "\n"Output :10)Write program to bubble sort the given numbers? .data.align 4Table: .space 24msg1: .asciiz "Please insert an integer: "msg2: .asciiz " "msg3: .asciiz "\nVector contents: ".text.globl mainmain:addi $s0,$zero,5addi $t0,$zero,0in:la $a0,msg1syscallli $v0,5syscalladd $t1,$t0,$zerosll $t1,$t0,2add $t3,$v0,$zero sw $t3,Table ( $t1 ) addi $t0,$t0,1slt $t1,$s0,$t0beq $t1,$zero,inla $a0,Tableaddi $a1,$s0,1 #a1=6 #call buble_sortjal buble_sort#print tableli $v0,4la $a0,msg3syscallla $t0,Tableadd $t1,$zero,$zeroprinttable:lw $a0,0($t0)li $v0,1syscallli $v0,4la $a0,msg2syscalladdi $t0,$t0,4addi $t1,$t1,1slt $t2,$s0,$t1beq $t2,$zero,printtableli $v0,10syscallbuble_sort:#a0=address of table#a1=sizeof tableadd $t0,$zero,$zero #counter1( i )=0 loop1:addi $t0,$t0,1 #i++bgt $t0,$a1,endloop1 #if t0 < a1 break; add $t1,$a1,$zero #counter2=size=6 loop2:bge $t0,$t1,loop1 #j < = i#slt $t3,$t1,$t0#bne $t3,$zero,loop1addi $t1,$t1,-1 #j--mul $t4,$t1,4 #t4+a0=table[j]addi $t3,$t4,-4 #t3+a0=table[j-1]add $t7,$t4,$a0 #t7=table[j]add $t8,$t3,$a0 #t8=table[j-1]lw $t5,0($t7)lw $t6,0($t8)bgt $t5,$t6,loop2#switch t5,t6sw $t5,0($t8)sw $t6,0($t7)j loop2endloop1:jr $raOutput:11)Write a program to comparing strings whether or not ? .datamsg1:.asciiz "Please insert text (max 20 characters): "msg2:.asciiz "\nNOT SAME"msg3:.asciiz "\nSAME"str1: .space 20str2: .space 20.text.globl mainmain:addi $v0,4la $a0,msg1syscallli $v0,8la $a0,str1addi $a1,$zero,20syscall #got string 1li $v0,4la $a0,msg1syscallli $v0,8la $a0,str2addi $a1,$zero,20syscall #got string 2la $a0,str1 #pass address of str1 la $a1,str2 #pass address of str2 jal strcmp #call strcmpbeq $v0,$zero,ok #check result li $v0,4la $a0,msg2syscallj exitok:li $v0,4la $a0,msg3syscallexit:li $v0,10syscallstrcmp:add $t0,$zero,$zeroadd $t1,$zero,$a0add $t2,$zero,$a1loop:lb $t3($t1) #load a byte from each stringlb $t4($t2)beqz $t3,checkt2 #str1 endbeqz $t4,missmatchslt $t5,$t3,$t4 #compare two bytesbnez $t5,missmatchaddi $t1,$t1,1 #t1 points to the next byte of str1 addi $t2,$t2,1j loopmissmatch:addi $v0,$zero,1j endfunctioncheckt2:bnez $t4,missmatchadd $v0,$zero,$zeroendfunction:jr $raOutput :12)Write a program convert lowercase letters to uppercase letters? .dataname_prompt: .asciiz "Please type your name: "out_msg: .asciiz "Your name in capitals is: "in_name: .space 31 # space for input string.text.globl mainmain:la $a0,name_prompt # print prompt stringli $v0,4syscallla $a0,in_name # read the input stringli $a1,31 # at most 30 chars + 1 null charli $v0,8syscallla $a0,out_msg # write output messageli $v0,4syscallla $t0,in_nameloop:lb $t1,($t0)beqz $t1,exit_loop # if NULL, we are doneblt $t1,'a',no_changebgt $t1,'z',no_changeaddiu $t1,$t1,-32 # convert to uppercase: 'A'-'a'=-32 sb $t1,($t0)no_change:addiu $t0,$t0,1 # increment pointerj loopexit_loop:la $a0,in_name # output converted stringli $v0,4syscallli $v0,10 # exitsyscallOutput:13)Write a program for multiplication table? .textmain:## read A into $t0, B into $t1.li $v0, 5 # syscall 5 = read_intsyscallmove $t0, $v0 # A = integer just readli $v0, 5 # syscall 5 = read_intsyscallmove $t1, $v0 # B = integer just readblez $t1, exit # if B <= 0, exit.mul $t2, $t0, $t1 # S = A * B.move $t3, $t0 # m = Aloop:move $a0, $t3 # print m.li $v0, 1 # syscall 1 = print_intsyscall # make the system call.beq $t2, $t3, endloop # if m == S, we're done.add $t3, $t3, $t0 # otherwise, m = m + A.la $a0, space # print a space.li $v0, 4 # syscall 4 = print_stringsyscallb loop # iterate.endloop:la $a0, newline # print a newline:li $v0, 4 # syscall 4 = print_stringsyscallexit: # exit the program:li $v0, 10 # syscall 10 = exitsyscall # we're outta here.## Here's where the data for this program is stored: .dataspace: .asciiz " "newline: .asciiz "\n"Output:14)Write a program to find given number is even or odd? .dataeven: .asciiz "That number is even"odd: .asciiz "That number is odd".globl main.textmain:#Print to screen the string “Please enter an integer: “li $v0, 4la $a0, questionsyscall#Input an integer from keyboard and store it in register $v0li $v0, 5syscall#Move content of $v0 into register $t0move $t0, $v0#Load register $t1 with immediate value of 2li $t1, 2#Divide $t0 by $t1.#$t0 % $t1 will be stored in $HI. $t0 * $t1 will be stored in $LOdiv $t0, $t1#Move content of $HI into register $t2mfhi $t2#If register $t2 is 0 (NUM % 2 = 0) then branch to AAAbeq $t2, $zero, AAA#Print to screen string “That number is odd” if haven’t branched li $v0, 4la $a0, oddsyscall#Jump to BBB (to skip message for even number)j BBB#AAA startAAA:#Print to screen stri ng “That number is even”li $v0, 4la $a0, evensyscall#BBB startBBB:Output :15)Write a program for palindrome number? ## Register usage:## $t1 - A.## $t2 - B.## $t3 - the character at address A.## $t4 - the character at address B.## $v0 - syscall parameter / return values.## $a0 - syscall parameters.## $a1 - syscall parameters..textmain: # SPIM starts by jumping to main.## read the string S:la $a0, string_spaceli $a1, 1024li $v0, 8 # load "read_string" code into $v0.syscallla $t1, string_space # A = S.la $t2, string_space ## we need to move B to the endlength_loop: # of the string:。

201010101MIPS汇编语...

201010101MIPS汇编语...

z e.g., jr $31 我们就回到了 0x0040001c
subu $s1,$s1,1 bnez $s1,loop #f=n! j result
.data 0x10000200
0x10000200 n: .word 4
2010-10-10
0x10000204 f: .word 0
17
编程指南:(2)分支
.text 0x00400100
fact: sw $ra,0($s7)
addiu $s0,$0,1
lw $s1,0($s4) #$s0=n!
loop: mul $s0,$s1,$s0
subu $s1,$s1,1
bnez $s1,loop #f=n!
j result
.data 0x10000200
n: .word 4
lw $s1,0($s4)
#$s0=n!
loop: mul $s0,$s1,$s0
subu $s1,$s1,1
bnez $s1,loop #f=n!
j result
#$s7为$ra开辟一个地址空间
.data 0x10000200
beq $0,$0, fact result: sw $s0,0($s5)
.text
#将子串即指令或字送入用户文件段
.global main #必须为全局变量
Main: lw $t0, item
2010-10-10
MIPS汇编中的命令见Page625 8
MIPS汇编器:存储器中位置
{ 汇编语言源文件:.s
{ 特殊符号 “.” (点): 表示当前 位置 z “.” MIPS汇编命令标识符 z “label:”
MIPS汇编语言程序设计

MIPS汇编范例

MIPS汇编范例

mips汇编语言之实现swap函数收藏此程序用来交换两个整数已在pcspim下编译通过######################################################### programed by stevie zou #### purpose:to swap two values ###### 10-15-2008 ######### ############################### text segment ###############.text.globl mainmain: la $t0, number #读取两个整数并放入寄存器$t1,$t2 lw $t1, 0($t0)lw $t2, 4($t0)li $v0, 4 #打印msg1la $a0, msg1syscallli $v0, 1 #打印转换前$t1中的值move $a0, $t1syscallli $v0, 4 #打印msg2la $a0, msg2syscallli $v0, 1 #打印转换前$t2中的值move $a0, $t2syscallmove $t3, $t1 #关键部分,在寄存器间move数据move $t1, $t2move $t2, $t3li $v0, 4 #打印msg3la $a0, msg3syscallli $v0, 1 #打印转换后$t1中的值move $a0, $t1syscallli $v0, 4 #打印换行符/nla $a0, msg4syscallli $v0, 1 #打印转换后$t2中的值move $a0, $t2syscall########### data segment ##############.datanumber: .word 12,34msg1: .asciiz "the first number is:\n"msg2: .asciiz "\nthe second number is:\n"msg3: .asciiz "\nnow they are swapped as:\n"msg4: .ascii "\n"## end of file程序运行结果为:本文来自CSDN博客,转载请标明出处:/yihaozou/archive/2008/10/19/3081322.aspxmips汇编简单实例——一个小计算器收藏其实开始的时候一直在看mips的指令格式,看了、忘了,没什么效果。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

mips汇编语言之实现swap函数收藏此程序用来交换两个整数已在pcspim下编译通过######################################################### programed by stevie zou #### purpose:to swap two values ###### 10-15-2008 ######### ############################### text segment ###############.text.globl mainmain: la $t0, number #读取两个整数并放入寄存器$t1,$t2 lw $t1, 0($t0)lw $t2, 4($t0)li $v0, 4 #打印msg1la $a0, msg1syscallli $v0, 1 #打印转换前$t1中的值move $a0, $t1syscallli $v0, 4 #打印msg2la $a0, msg2syscallli $v0, 1 #打印转换前$t2中的值move $a0, $t2syscallmove $t3, $t1 #关键部分,在寄存器间move数据move $t1, $t2move $t2, $t3li $v0, 4 #打印msg3la $a0, msg3syscallli $v0, 1 #打印转换后$t1中的值move $a0, $t1syscallli $v0, 4 #打印换行符/nla $a0, msg4syscallli $v0, 1 #打印转换后$t2中的值move $a0, $t2syscall########### data segment ##############.datanumber: .word 12,34msg1: .asciiz "the first number is:\n"msg2: .asciiz "\nthe second number is:\n"msg3: .asciiz "\nnow they are swapped as:\n"msg4: .ascii "\n"## end of file程序运行结果为:本文来自CSDN博客,转载请标明出处:/yihaozou/archive/2008/10/19/3081322.aspxmips汇编简单实例——一个小计算器收藏其实开始的时候一直在看mips的指令格式,看了、忘了,没什么效果。

现在通过这个例子,简单训练一下:mips汇编中:常用寄存器的使用;系统调用:1(print_int),4(print_str),5(read_int),10(exit);算术运算:add,sub,mul,div;逻辑运算及跳转;上代码:(汇编写的少,代码书写、风格还很差劲,感谢请大虾指教):view plaincopy to clipboardprint?#sa mple example 'a small calculater’.text # text section.globl main # call main by SPIMmain:la $t0, value # load address "value" into $t0la $a0,msg0li $v0,4syscall #print "please choose the operation:"li $v0, 5syscallsw $v0, 8($t0)la $a0,msg1li $v0,4syscall #print "first num:"li $v0, 5syscallsw $v0, 0($t0)la $a0,msg2li $v0,4syscall #print " second num:"li $v0, 5syscallsw $v0, 4($t0) #read the other numla $a0,newlineli $v0,4syscall #print "\n"lw $t1, 0($t0) # load the first numlw $t2, 4($t0) # load the second num lw $t3, 8($t0) # load the operationbeq $t3,1,addOp # if +beq $t3,2,subOp # if -beq $t3,3,mulOp # if *beq $t3,4,divOp # if /addOp:add $t4, $t1, $t2 # $t1 + $t2 = $t4sw $t4, 12($t0) #la $t5,addFlagj printResultsubOp:sub $t4, $t1, $t2 # $t1 - $t2 = $t4sw $t4, 12($t0)la $t5,subFlagj printResultmulOp:mul $t4, $t1, $t2 # $t1 * $t2 = $t4sw $t4, 12($t0)la $t5,mulFlagj printResultdivOp:div $t4, $t1, $t2 # $t1 / $t2 = $t4sw $t4, 12($t0)la $t5,divFlagj printResultprintResult:lw $a0,0($t0)li $v0,1syscall #read first numberla $a0,0($t5)li $v0,4syscall #print opflaglw $a0,4($t0)li $v0,1syscall #print second numberla $a0,equalStrli $v0,4syscall #print " = "lw $a0,12($t0)li $v0,1syscall # print sum resultj exitexit:la $a0,newlineli $v0,4syscall #print " \n "li $v0,10syscall # exit# data section.datavalue: .word 0, 0, 0 ,0 ,0 # 0: first num ,4 : second num , 8 : operation , 12:resultmsg0 : .asciiz " please choose the operation(1~4):\n\t\t1 : +,addition \n\t\t2 : -,subtracter\n\t\t3 : * multiplication \n\t\t4 : /,division\n"msg1 : .asciiz "first num:"msg2 : .asciiz "second num:"addFlag : .asciiz " + "subFlag : .asciiz " - "mulFlag : .asciiz " * "divFlag : .asciiz " / "equalStr : .asciiz " = "newline : .asciiz "\n===============================\n"#sample example 'a small calculater’.text # text section.globl main # call main by SPIMmain:la $t0, value # load address "value" into $t0la $a0,msg0li $v0,4syscall #print "please choose the operation:"li $v0, 5sw $v0, 8($t0)la $a0,msg1li $v0,4syscall #print "first num:"li $v0, 5syscallsw $v0, 0($t0)la $a0,msg2li $v0,4syscall #print " second num:"li $v0, 5syscallsw $v0, 4($t0) #read the other numla $a0,newlineli $v0,4syscall #print "\n"lw $t1, 0($t0) # load the first numlw $t2, 4($t0) # load the second numlw $t3, 8($t0) # load the operationbeq $t3,1,addOp # if +beq $t3,2,subOp # if -beq $t3,3,mulOp # if *beq $t3,4,divOp # if /addOp:add $t4, $t1, $t2 # $t1 + $t2 = $t4sw $t4, 12($t0) #la $t5,addFlagj printResultsubOp:sub $t4, $t1, $t2 # $t1 - $t2 = $t4sw $t4, 12($t0)la $t5,subFlagj printResultmulOp:mul $t4, $t1, $t2 # $t1 * $t2 = $t4sw $t4, 12($t0)la $t5,mulFlagj printResultdivOp:div $t4, $t1, $t2 # $t1 / $t2 = $t4sw $t4, 12($t0)la $t5,divFlagj printResultprintResult:lw $a0,0($t0)li $v0,1syscall #read first numberla $a0,0($t5)li $v0,4syscall #print opflaglw $a0,4($t0)li $v0,1syscall #print second numberla $a0,equalStrli $v0,4syscall #print " = "lw $a0,12($t0)li $v0,1syscall # print sum resultj exitexit:la $a0,newlineli $v0,4syscall #print " \n "li $v0,10syscall # exit# data section.datavalue: .word 0, 0, 0 ,0 ,0 # 0: first num ,4 : second num , 8 : operation , 12:resultmsg0 : .asciiz " please choose the operation(1~4):\n\t\t1 : +,addition \n\t\t2 : -,subtracter\n\t\t3 : * multiplication \n\t\t4 : /,division\n"msg1 : .asciiz "first num:"msg2 : .asciiz "second num:"addFlag : .asciiz " + "subFlag : .asciiz " - "mulFlag : .asciiz " * "divFlag : .asciiz " / "equalStr : .asciiz " = "newline : .asciiz "\n===============================\n"运行效果截图如下:本文来自CSDN博客,转载请标明出处:/oncoding/archive/2009/07/15/4346531.aspx本文来自CSDN博客,转载请标明出处:/oncoding/archive/2009/07/15/4346531.aspx高精度整数除法(MIPS汇编)收藏.datatitle: .asciiz "\t\t高精度除法演示程序\n"tip1: .asciiz "\n请输入被除数:"tip2: .asciiz "请输入除数:"tip3: .asciiz "要输出小数点后多少位:"tip4: .asciiz "\n是否继续?(y/n)"err: .asciiz "\n除数不能为0,请重新输入\n"dot: .asciiz "."sign: .asciiz "-"buf: .byte 0:16.text.globl __start__start:#输出标题li $v0, 4la $a0, titlesyscall#提示输入被除数并接收输入restart:li $v0, 4la $a0, tip1syscallli $v0, 5syscallmove $t0, $v0#提示输入除数并接收输入input_again:li $v0, 4la $a0, tip2syscallli $v0, 5syscallmove $t1, $v0#检测除数是否为0,是则输出错误信息并跳转到除数输入处,否则继续执行bnez $t1, nextli $v0, 4la $a0, errsyscallj input_again#提示输入要求的小数位数并接收输入next:li $v0, 4la $a0, tip3syscallli $v0, 5syscallmove $t2, $v0#两数进行除法运算,把运算结果保存到寄存器$t8和$t9div $t0, $t1move $s0, $zeromflo $t8mfhi $t9#检测商是否为0,是则继续执行下一步,否则跳转到输出整数结果处bnez $t8, int_result#检测被除数是否为非负数,是则检查除数,否则置$s0为1,$s0用来记录负数的个数bgez $t0, next_checkaddi $s0, $zero, 1#检测除数是否为非负数,是则输出-号,否则$s0加1next_check:bgez $t1, print_signaddi $s0, $s0, 1#输出-号,如果$s0值不为1,即负数个数不为奇数,则跳过此步print_sign:bne $s0, 1, int_resultli $v0, 4la $a0, signsyscall#输出整数部分结果int_result:li $v0, 1move $a0, $t8syscall#输出小数点li $v0, 4la $a0, dotsyscall#检测余数是否不小于0,是则跳转到下一步执行,否则取反bgez $t9, next_numneg $t9, $t9#检测除数是否不小于0,是则转入下一步,否则取反next_num:bgez $t1, isloopneg $t1, $t1#余数乘以10,再除以除数,得到新的商和余数isloop:move $t8, $t9mul $t8, $t8, 10div $t8, $t1mflo $t8mfhi $t9#输出新的商li $v0, 1move $a0, $t8syscall#小数位数记录减1,当其大于0时继续回到上一标号处执行,否则转入下一步sub $t2, $t2, 1bgtz $t2, isloop#显示提示信息是否要重新开始,li $v0, 4la $a0, tip4syscall#接收字符输入li $v0, 8la $a0, bufli $a1, 16syscall#输入的第一字符为y时则重新开始, 否则结束程序lbu $t3, bufbeq $t3, 121, restartli $v0, 10syscall本文来自CSDN博客,转载请标明出处:/masm32v9/archive/2010/02/28/5331042.aspx。

相关文档
最新文档