Kip Irvine 汇编语言 基于x86处理器 Chapter01 代码(含习题)

习题

1.7.2 01

#include<iostream>
#include<string>

using namespace std;

int stringToInt(string s)
{
	int res = 0;
	if (s.size() == 16)
	{
		res = s[0] - '0';
		int i = 1;
		while (i < s.size())
		{
			res = res*2+(int)(s[i] - '0');
			i++;
		}
	}
	return res;
}

int main()
{
	string s;
	cin >> s;
	cout << stringToInt(s);
	system("pause");
	return 0;
}

1.7.2 02


#include<iostream>
#include<string>

using namespace std;

long long stringToInt(string s)
{
	long long res = 0;
	int i = 0;
	if (s.size() == 32)
	{
		while (i < 32)
		{
			if (s[i] == '0')
			{
				res = res * 16 + 0;
			}
			if (s[i] == '1')
			{
				res = res * 16 + 1;
			}
			if (s[i] == '2')
			{
				res = res * 16 + 2;
			}
			if (s[i] == '3')
			{
				res = res * 16 + 3;
			}
			if (s[i] == '4')
			{
				res = res * 16 + 4;
			}
			if (s[i] == '5')
			{
				res = res * 16 + 5;
			}
			if (s[i] == '6')
			{
				res = res * 16 + 6;
			}
			if (s[i] == '7')
			{
				res = res * 16 + 7;
			}
			if (s[i] == '8')
			{
				res = res * 16 + 8;
			}
			if (s[i] == '9')
			{
				res = res * 16 + 9;
			}
			if (s[i] == 'A' || s[i] == 'a')
			{
				res = res * 16 + 10;
			}
			if (s[i] == 'B' || s[i] == 'b')
			{
				res = res * 16 + 11;
			}
			if (s[i] == 'C' || s[i] == 'c')
			{
				res = res * 16 + 12;
			}
			if (s[i] == 'D' || s[i] == 'd')
			{
				res = res * 16 + 13;
			}
			if (s[i] == 'E' || s[i] == 'e')
			{
				res = res * 16 + 14;
			}
			if (s[i] == 'F' || s[i] == 'f')
			{
				res = res * 16 + 15;
			}
			i++;
		}
		
	}
	return res;
}

int main()
{
	string s;
	cin >> s;
	cout << stringToInt(s);
	system("pause");
	return 0;
}

1.7.2 03

#include<iostream>
#include<string>

using namespace std;

string ToString(int x)
{
	string res = "";
	while (x)
	{
		res += to_string(x%2);
		x /= 2;
	}
	reverse(res.begin(),res.end());
	return res;
}

int main()
{
	int x;
	cin >> x;
	cout << ToString(x);
	system("pause");
	return 0;
}

1.7.2 04

#include<iostream>
#include<string>

using namespace std;

char sign[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

string ToString(int x)
{
	string res = "";
	while (x)
	{
		res += sign[x % 16];
		x /= 16;
	}
	reverse(res.begin(), res.end());
	return res;
}

int main()
{
	int x;
	cin >> x;
	cout << ToString(x);
	system("pause");
	return 0;
}

1.7.2 05

import java.util.*;

public class solve {

    static String addByBase(String a, String b, int base) {

        StringBuffer ans = new StringBuffer();

        int n = Math.max(a.length(), b.length()), carry = 0;

        for (int i = 0; i < n; i++) {
            carry += i < a.length() ? (a.charAt(a.length() - 1 - i) - '0') : 0;
            carry += i < b.length() ? (b.charAt(b.length() - 1 - i) - '0') : 0;
            ans.append((char) (carry % base + '0'));
            carry /= base;
        }

        if (carry > 0) {
            ans.append('1');
        }
        ans.reverse();

        return ans.toString();
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next(); 
        String b = sc.next(); 
        int base = sc.nextInt(); 
        System.out.println(addByBase(a, b, base));
    }

}

1.7.2 06

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner s = new Scanner(System.in);

        while (s.hasNext()) {
            long i = Long.parseLong(s.next().replaceAll("\\+", ""), 16) +

                    Long.parseLong(s.next().replaceAll("\\+", ""), 16);

            if (i < 0)

                System.out.println("-" + Long.toHexString(-i).toUpperCase());

            else

                System.out.println(Long.toHexString(i).toUpperCase());

        }

    }

}

1.7.2 07

#include<cstdio>
using namespace std;
int main()
{
    __int64 a, b, sum;
    while (scanf_s("%I64X%I64X", &a, &b) != EOF)
    {
        sum = a * b;
        if (sum >= 0)
            printf("%I64X\n", sum);
        else
            printf("-%I64X\n", -sum);
    }
    return 0;
}

1.7.2 08

Java源代码
public class test {
    public static void main(String[] args){
        int Y =0 ;
        int X = (Y + 4) * 3;
    }
}
Java源代码反汇编后的结果
public class test {
  public test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_0
       1: istore_1
       2: iload_1
       3: iconst_4
       4: iadd
       5: iconst_3
       6: imul
       7: istore_2
       8: return
}

1.7.2 09

#include <stdlib.h>
#include <stdio.h>


int main()
{
    unsigned int a = 1;
    unsigned int b = -1;

    printf("a is 0x%X\n", a);
    printf("b is 0x%X\n", b);

    printf("a-b is 0x%llX\n", (unsigned long long)a - (unsigned long long)b);

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值