How C language is converted into a binary language of the underlying computer (MIPS)

Beginning of the article I just want to ask a question: write a lot of code, you're not curious about the underlying computer code is what you?

The following step by step to explore this process. First of all, we need to have a preliminary understanding, once left to right compiler , assembler.

High-level language (C language) binary machine language assembly language

int add(int a,int b){                           add: 
    int c;                                                    add   $a2,$a0,$a1  //c=a+b                      00000000100001010011000000100000 
    c = a + b;                  -->编译               add  $v0,$a2,$zero                -->汇编       00000000110000000001000000100000
    return c;                                               jr       $ra                                                   00000011111000000000000000001000
}

About $ a2, $ ra register and so on, we can look at this blog article summarizes the main MIPS general-purpose registers  .

First we need to understand the concept of a computer - the process , meaning that the subroutine parameters provided to perform certain tasks stored. Plainly, the above-described process is the C language which add function logic executing in the CPU. 32 MIPS registers dispensing procedure call is to follow the following conventions:

  • $ a0 ~ $ a3: used to pass parameters four parameter register. As described above A0 is the $ A, $ B A1 that is, since the parameter register is also sufficient, so  $ A2 is the c.
  • $ V0 ~ $ v1: two for the return value registers.
  • $ ra: return start point for returning the address register. As described above jr $ ra, meaning that an unconditional jump to the address register established. In layman's terms, $ ra is pre-stored procedure call instruction address, after performing the process jumps to instruction address before the call process, continue to do follow-up calculations. int a1 = add (1,2), add After the process execution logic, jr $ ra continues back calculated from a1. Because the process is likely to call a program in multiple points . I.e. int a2 = add (3,4); jr $ ra After execution continues back a2 calculated. Code may be as follows:

#include <stdio.h>

int add(int a,int b){
    int c;
    c = a + b;
    return c;
}

void main(){
    int a1 = add(1,2);
    int a2 = add(3,4);
}

In summary, we should know why that is, $ a0 a, $ a1 is the b, $ a2 is the c a. Next, we continue, how assembly language into binary programming language. Surely we all know, no matter what the program, the last 01 are present in the bottom of the computer.

Next, we continue, in comparison Everyone wants to know how the assembly language is converted into binary machine language. The following is a MIPS instruction and the meaning of each field name. Type I and R is divided into two types instruction format.

Type R : a register, performing a logic operation, such as addition and subtraction operations.

on rs rt rd Smt funct
# 6 5 5 5 5 # 6
  •  op: basic operating instructions, commonly referred to as operation code (opcode).
  • rs: a first source operand registers.
  • rt: operating a second source register number.
  • rd: destination register for storing operation results.
  • shamt: the amount of displacement.
  • funct: function code (function code).

Type the I : for immediate, i.e. immediate data transfer instruction, such as SW, LW and other operations.

on rs rt constant and adress
# 6 5 5 # 16

 Here are common values ​​MPIS instructions to give everyone a look:

 instruction

format

on

rs

rt

rd

many

hinct

address

add

R

0

reg

reg

reg

0

32

n. a.

Iw (load word)

I

35

reg

n. a

n. a n. a n. a address

In the above table, "reg" register a reference numeral (from 0 ~ 31), "address" represents the 16-bit address, "na." (Not applicable) indicates that the field is not present in the instruction format. Note that, add hardware instruction operation performed is determined based on the value of field hmct: add (32) or substract (34)

Therefore, for the above conversion, you can get:

    Assembly language                                             

 add: 

  add  $a2,$a0,$a1  //c=a+b                    
  add  $v0,$a2,$zero              
  jr      $ra        
}

on

rs

rt

rd

address/shumt

hinct

0

4

5

6

0

32

6

0

2 0 32
0 31 0 0 0 8

Converted to binary:

on

rs

rt

rd

address/shumt

hinct

000000

00100

00101

00110

00000

100000 

000000 

00110

00000

00010 00000 100000
000000 11111 00000 00000 00000 001000

Here, we will have a little bit with feeling, a real program of instruction is involved in a lot of instruction computing. Far more than three, so we need to learn to strengthen and deepen the understanding of the computer. 

references:

[1] Computer Organization and Design: hardware / software interface (the original book 5th edition) Beijing: Mechanical Industry Press, 2015.

发布了3 篇原创文章 · 获赞 0 · 访问量 69

Guess you like

Origin blog.csdn.net/taipoucha5799/article/details/104956366