Mit6.004-lab06-Procedures And Stacks

一、 BETA ISA

A:0、1、2、3、0xc
B:0x2000、0xEDEDEDED、0xFEDEDEDE、0x2004、110000 00000 11111 0010 0000 0000 0000(0xc01f2000)
C:0x87654321、1、0x87654320、0x14、011101 11111 00001 0000 0000 0000 0010(0x77e10002)
D:17、0、32、no instructions need to be changed
E:4、110000 00000 00000 0000 0000 0000 0000(0xC0000000)

二、 快速排序

实现代码

	p_array=R2          // base address of array (arg 0)
    p_left=R3
    p_right=R4
    p_pivotIndex=R5     // Corresponds to PivotIndex in C program
    p_pivotValue=R6
    p_storeIndex=R7
partition:		
        PUSH(LP)
        PUSH(BP)
        MOVE(SP, BP)
		PUSH(R1)
		PUSH(R2)
		PUSH(R3)
		PUSH(R4)
		PUSH(R5)
		PUSH(R6)
		PUSH(R7)
		PUSH(R8)
		PUSH(R9)
		
		LD(BP, -12, p_array)
		LD(BP, -16, p_left)
		LD(BP, -20, p_right)
		ADD(p_left, p_right, p_pivotIndex)
		SHRC(p_pivotIndex, 1, p_pivotIndex)
		MULC(p_pivotIndex, 4, R9)
		ADD(p_array, R9, R9)		
		LD(R9, 0, p_pivotValue)
		MULC(p_right, 4, R9)
		ADD(p_array, R9, R9)
		LD(R9, 0, R7)
		MULC(p_pivotIndex, 4, R9)
		ADD(p_array, R9, R9)
		ST(R7, 0, R9)
		MOVE(p_left, p_storeIndex)
START:	ADDC(p_left, 1, p_left)
		CMPLE(p_left, p_right, R8)
		BF(R8, AFTER)
		MULC(p_left, 4, R9)
		ADDC(R9, -4, R9)
		ADD(p_array, R9, R9)
		LD(R9, 0, R8)		
		CMPLE(R8, p_pivotValue, R1)
		BF(R1, START)
		MULC(p_storeIndex, 4, R9)
		ADD(p_array, R9, R9)
		LD(R9, 0, R1)
		MULC(p_left, 4, R9)
		ADDC(R9, -4, R9)
		ADD(p_array, R9, R9)
		ST(R1, 0, R9)
		MULC(p_storeIndex, 4, R9)
		ADD(p_array, R9, R9)
		ST(R8, 0, R9)		
		ADDC(p_storeIndex, 1, p_storeIndex)
		BR(START)
AFTER:  MULC(p_storeIndex, 4, R9)
		ADD(p_array, R9, R9)
		LD(R9, 0, R8)
		MULC(p_right, 4, R9)
		ADD(p_array, R9, R9)
		ST(R8, 0, R9)
		MULC(p_storeIndex, 4, R9)
		ADD(p_array, R9, R9)
		ST(p_pivotValue, 0, R9)
		MOVE(p_storeIndex, R0)
        POP(R9)
		POP(R8)
		POP(R7)
		POP(R6)
		POP(R5)
		POP(R4)
		POP(R3)
		POP(R2)
		POP(R1)
		
		MOVE(BP, SP)
        POP(BP)
        POP(LP)
        JMP(LP)
quicksort:
        PUSH(LP)
        PUSH(BP)
        MOVE(SP, BP)
		PUSH(R1)
		PUSH(R2)
		PUSH(R3)
		PUSH(R4)
		
		LD(BP, -12, R1)
		LD(BP, -16, R2)
		LD(BP, -20, R3)
		.breakpoint
		CMPLT(R2, R3, R4)
		BF(R4, END)
		PUSH(R3)
		PUSH(R2)
		PUSH(R1)
		BR(partition, LP)
		DEALLOCATE(3)
		.breakpoint
		ADDC(R0, -1, R4)
		PUSH(R4)
		PUSH(R2)
		PUSH(R1)
		BR(quicksort, LP)
		DEALLOCATE(3)
		
		PUSH(R3)
		ADDC(R0, 1, R4)
		PUSH(R4)
		PUSH(R1)
		BR(quicksort, LP)
		DEALLOCATE(3)
		
END:	POP(R4)
		POP(R3)
		POP(R2)
		POP(R1)
        MOVE(BP, SP)
        POP(BP)
        POP(LP)
        JMP(LP)

A:0x14
B:0、-1
C:0xa
D:first (smaller elements)
E:0、4
F:5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值