2.63-将下面的C函数代码补充完整。函数srl用算术右移(由值xsra给出)来完成逻辑右移,后面的其他操作不包括右移或者除法。

接题目:

函数sra用逻辑右移(由值xsrl给出)来完成算术右移,后面的其他操作不包括右移或者除法。可以通过计算8*sizeof(int)来确定数据类型int中的位数w。位移量k的取值范围为0~w-1。

unsigned srl(unsigned x, int k) {
	/* Perform shift arithmatically */
	unsigned xsra = (int)x >> k;
	.
	.
	.
}

int sra(int x, int k) {
	/* Perform shift logically */
	int xsrl = (unsigned)x >> k;
	.
	.
	.
}

答案

#include <stdio.h>
#include <assert.h>

unsigned srl(unsigned x, int k) { 
	unsigned xsra = (int)x >> k; 
	int w = sizeof(int) << 3; 
	int mask = (int)-1 << (w - k); 
	return xsra & ~mask; 
}

int sra(int x, int k) {
		int xsrl = (unsigned)x >> k; 
		int w = sizeof(int) << 3;
		int mask = (int)-1 << (w - k); 
		//let mask remain unchanged when the first bit of x is 1, otherwise 0. 
		int m = 1 << (w - 1); 
		mask &= ! (x & m) - 1; 
		return xsrl | mask; 
}

int main(int argc, char* argv[]) {
	unsigned test_unsigned = 0x12345678; 
	int test_int = 0x12345678; 
	assert(srl(test_unsigned, 4) == test_unsigned >> 4);
	assert(sra(test_int, 4) == test_int >> 4);
	test_unsigned = 0x87654321; 
	test_int = 0x87654321; 
	assert(srl(test_unsigned, 4) == test_unsigned >> 4); 
	assert(sra(test_int, 4) == test_int >> 4);
	return 0;
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是用MIPS汇编语言实现8种算术运算和8种逻辑运算的代码,并将它们整合在一起: ``` .data prompt1: .asciiz "Enter the first number: " prompt2: .asciiz "Enter the second number: " addition: .asciiz "Addition result: " subtraction: .asciiz "Subtraction result: " multiplication: .asciiz "Multiplication result: " division: .asciiz "Division result: " modulus: .asciiz "Modulus result: " and: .asciiz "And result: " or: .asciiz "Or result: " xor: .asciiz "Xor result: " nand: .asciiz "Nand result: " nor: .asciiz "Nor result: " xnor: .asciiz "Xnor result: " shift_left: .asciiz "Shift left result: " shift_right: .asciiz "Shift right result: " .text main: # Addition li $v0, 4 la $a0, prompt1 syscall li $v0, 5 syscall move $t0, $v0 li $v0, 4 la $a0, prompt2 syscall li $v0, 5 syscall move $t1, $v0 add $t2, $t0, $t1 li $v0, 4 la $a0, addition syscall li $v0, 1 move $a0, $t2 syscall # Subtraction li $v0, 4 la $a0, prompt1 syscall li $v0, 5 syscall move $t0, $v0 li $v0, 4 la $a0, prompt2 syscall li $v0, 5 syscall move $t1, $v0 sub $t2, $t0, $t1 li $v0, 4 la $a0, subtraction syscall li $v0, 1 move $a0, $t2 syscall # Multiplication li $v0, 4 la $a0, prompt1 syscall li $v0, 5 syscall move $t0, $v0 li $v0, 4 la $a0, prompt2 syscall li $v0, 5 syscall move $t1, $v0 mult $t0, $t1 mflo $t2 li $v0, 4 la $a0, multiplication syscall li $v0, 1 move $a0, $t2 syscall # Division li $v0, 4 la $a0, prompt1 syscall li $v0, 5 syscall move $t0, $v0 li $v0, 4 la $a0, prompt2 syscall li $v0, 5 syscall move $t1, $v0 div $t0, $t1 mflo $t2 li $v0, 4 la $a0, division syscall li $v0, 1 move $a0, $t2 syscall # Modulus li $v0, 4 la $a0, prompt1 syscall li $v0, 5 syscall move $t0, $v0 li $v0, 4 la $a0, prompt2 syscall li $v0, 5 syscall move $t1, $v0 div $t0, $t1 mfhi $t2 li $v0, 4 la $a0, modulus syscall li $v0, 1 move $a0, $t2 syscall # And li $v0, 4 la $a0, prompt1 syscall li $v0, 5 syscall move $t0, $v0 li $v0, 4 la $a0, prompt2 syscall li $v0, 5 syscall move $t1, $v0 and $t2, $t0, $t1 li $v0, 4 la $a0, and syscall li $v0, 1 move $a0, $t2 syscall # Or li $v0, 4 la $a0, prompt1 syscall li $v0, 5 syscall move $t0, $v0 li $v0, 4 la $a0, prompt2 syscall li $v0, 5 syscall move $t1, $v0 or $t2, $t0, $t1 li $v0, 4 la $a0, or syscall li $v0, 1 move $a0, $t2 syscall # Xor li $v0, 4 la $a0, prompt1 syscall li $v0, 5 syscall move $t0, $v0 li $v0, 4 la $a0, prompt2 syscall li $v0, 5 syscall move $t1, $v0 xor $t2, $t0, $t1 li $v0, 4 la $a0, xor syscall li $v0, 1 move $a0, $t2 syscall # Nand li $v0, 4 la $a0, prompt1 syscall li $v0, 5 syscall move $t0, $v0 li $v0, 4 la $a0, prompt2 syscall li $v0, 5 syscall move $t1, $v0 and $t2, $t0, $t1 not $t2, $t2 li $v0, 4 la $a0, nand syscall li $v0, 1 move $a0, $t2 syscall # Nor li $v0, 4 la $a0, prompt1 syscall li $v0, 5 syscall move $t0, $v0 li $v0, 4 la $a0, prompt2 syscall li $v0, 5 syscall move $t1, $v0 or $t2, $t0, $t1 not $t2, $t2 li $v0, 4 la $a0, nor syscall li $v0, 1 move $a0, $t2 syscall # Xnor li $v0, 4 la $a0, prompt1 syscall li $v0, 5 syscall move $t0, $v0 li $v0, 4 la $a0, prompt2 syscall li $v0, 5 syscall move $t1, $v0 xor $t2, $t0, $t1 not $t2, $t2 li $v0, 4 la $a0, xnor syscall li $v0, 1 move $a0, $t2 syscall # Shift left li $v0, 4 la $a0, prompt1 syscall li $v0, 5 syscall move $t0, $v0 li $v0, 4 la $a0, prompt2 syscall li $v0, 5 syscall move $t1, $v0 sll $t2, $t0, $t1 li $v0, 4 la $a0, shift_left syscall li $v0, 1 move $a0, $t2 syscall # Shift right li $v0, 4 la $a0, prompt1 syscall li $v0, 5 syscall move $t0, $v0 li $v0, 4 la $a0, prompt2 syscall li $v0, 5 syscall move $t1, $v0 srl $t2, $t0, $t1 li $v0, 4 la $a0, shift_right syscall li $v0, 1 move $a0, $t2 syscall # Exit li $v0, 10 syscall ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

榆钱不知秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值