繁体   English   中英

将C程序转换为汇编代码

[英]Convert C program into assembly code

我究竟如何将这个 C 程序转换成汇编代码? 我很难理解这个过程或者如何开始它。 我是新来的。 任何帮助,将不胜感激!

while(a!=b){
     if(a > b){
        a = a - b;
       }
        else{
          b = b - a;
    }
  }
   return a;
   }

旁注:假设两个正整数 a 和 b 已经在寄存器 R0 和 R1 中给出。
你能留下评论解释你是如何做到的吗?

如果您使用的是gcc ,并且您的源代码是ac ,则可以将程序集作为gcc -S -o as ac ac 如果您使用的是 Visual Studio,则可以在调试时通过选择“反汇编”窗口来获取它。 这是 Visual Studio 的输出(我将子程序/函数命名为“common”,这就是为什么会出现“common”):

    while(a!=b){
    003613DE  mov         eax,dword ptr [a]  
    003613E1  cmp         eax,dword ptr [b]  
    003613E4  je          common+44h (0361404h)  
         if(a > b){
    003613E6  mov         eax,dword ptr [a]  
    003613E9  cmp         eax,dword ptr [b]  
    003613EC  jle         common+39h (03613F9h)  
            a = a - b;
    003613EE  mov         eax,dword ptr [a]  
    003613F1  sub         eax,dword ptr [b]  
    003613F4  mov         dword ptr [a],eax  
         }
         else{
    003613F7  jmp         common+42h (0361402h)  
             b = b - a;
    003613F9  mov         eax,dword ptr [b]  
    003613FC  sub         eax,dword ptr [a]  
    003613FF  mov         dword ptr [b],eax  
        }
      }
    00361402  jmp         common+1Eh (03613DEh)  
       return a;
    00361404  mov         eax,dword ptr [a]  
    }

这里变量a最初保存在内存中, bdword ptr [b] )也是如此。

教我系统编程的教授使用他所谓的“atomic-C”作为 C 和汇编之间的垫脚石。 atomic-C 的规则是(据我所知):

  1. 只允许简单的表达式,即a = b + c; 允许a = b + c + d; 不允许,因为那里有两个运营商。
  2. if 语句中只允许使用简单的布尔表达式,即允许if (a < b)但不允许if (( a < b) && (c < d))
  3. 只有 if 语句,没有 else 块。
  4. 不允许 for / while 或 do-while,只有 goto 和 label 的

所以,上面的程序将转化为;

 label1:
     if (a == b) 
         goto label2;

     if (a < b)
         goto label4;

     a = a - b;
     goto label3;

 label4:
     b = b - a;

 label3:
     goto label1; 

 label2:
     return a;

我希望我说对了……距离我上次不得不写 atomic-C 已经快二十年了。 现在假设以上是正确的,让我们开始将一些 atomic-C 语句转换为 MIPS(假设这是您正在使用的)程序集。 从 Elliott Frisch 提供的链接中,我们几乎可以立即翻译减法步骤:

a = a - b     becomes R0 = R0 - R1 which is: SUBU R0, R0, R1
b = b - a     becomes R1 = R1 - R0 which is: SUBU R1, R1, R0

由于 a 和 b 都是正整数,我使用了无符号减法。

可以这样进行比较:

if(a == b) goto label2 becomes if(R0 == R1) goto label2 which is: beq R0, R1, L2?

这里的问题是 beq 操作码的第三个参数是 PC 移动的位移。 在我们完成这里的手工组装之前,我们不会知道这个价值。

不平等是更多的工作。 如果我们离开伪代码指令,我们首先需要使用set on less than操作码的set on less than如果第一个寄存器小于第二个,则在目标寄存器中放置一个。 一旦我们这样做了,我们就可以像上面描述的那样branch on equal使用branch on equal

if(a < b)              becomes    slt R2, R0, R1  
    goto label4                   beq R2, 1, L4?        

跳转很简单,它们只是 j 和要跳转到的标签。 所以,

goto label1 becomes j label1

我们必须处理的最后一件事是退货。 返回是通过将我们想要的值移动到特殊寄存器 V0 然后跳转到调用此函数后的下一条指令来完成的。 问题是 MIPS 没有寄存器来注册移动命令(或者如果有,我已经忘记了),所以我们从寄存器移动到 RAM,然后再返回。 最后,我们使用保存返回地址的特殊寄存器 R31。

return a     becomes   var = a      which is SW R0, var
                       ret = var    which is LW var, V0
                       jump RA      which is JR R31

有了这些信息,程序就变成了。 而且我们还可以调整之前不知道的跳转:

           L1:
 0x0100        BEQ R0, R1, 8
 0x0104        SLT R2, R0, R1                 ; temp = (a < b)  temp = 1 if true, 0 otherwise
 0x0108        LUI R3, 0x01                   ; load immediate 1 into register R3
 0x010C        BEQ R2, 1, 2                   ; goto label4         
 0x0110        SUBU R0, R0, R1                ; a = a - b
 0x0114        J L3                           ; goto label3
           L4:
 0x0118        SUBU R1, R1, R0                ; b = b - a;
           L3:
 0x011C        J L1                           ; goto lable1
           L2:
 0x0120        SW R0, ret                     ; move return value from register to a RAM location
 0x0123        LW ret, V0                     ; move return value from RAM to the return register.
 0x0124        JR R31                         ; return to caller

自从我不得不做这样的事情以来已经快二十年了(现在,如果我需要组装,我只是按照其他人的建议去做,让编译器完成所有繁重的工作)。 我确信我在此过程中犯了一些错误,并且很乐意提供任何更正或建议。 我只参与了这个冗长的讨论,因为我将 OP 问题解释为手工翻译——有人在学习汇编时可能会这样做。

干杯。

我已将该代码转换为 16 位 NASM 程序集:

loop:
    cmp ax, bx
    je .end;        if A is not equal to B, then continue executing. Else, exit the loop
    jg greater_than;    if A is greater than B...

    sub ax, bx;     ... THEN subtract B from A...

    jmp loop;       ... and loop back to the beginning!

.greater_than:
    sub bx, ax;     ... ELSE, subtract A from B...

    jmp loop;       ... and loop back to the beginning!

.end:
    push ax;        return A

我用ax代替r0bx代替r1

ORG 000H                   // origin
MOV DPTR,#LUT              // moves starting address of LUT to DPTR
MOV P1,#00000000B          // sets P1 as output port
MOV P0,#00000000B          // sets P0 as output port
MAIN: MOV R6,#230D         // loads register R6 with 230D
      SETB P3.5            // sets P3.5 as input port
      MOV TMOD,#01100001B  // Sets Timer1 as Mode2 counter & Timer0 as Mode1 timer
      MOV TL1,#00000000B   // loads TL1 with initial value
      MOV TH1,#00000000B   // loads TH1 with initial value
      SETB TR1             // starts timer(counter) 1
BACK: MOV TH0,#00000000B   // loads initial value to TH0
      MOV TL0,#00000000B   // loads initial value to TL0
      SETB TR0             // starts timer 0
HERE: JNB TF0,HERE         // checks for Timer 0 roll over
      CLR TR0              // stops Timer0
      CLR TF0              // clears Timer Flag 0
      DJNZ R6,BACK
      CLR TR1              // stops Timer(counter)1
      CLR TF0              // clears Timer Flag 0
      CLR TF1              // clears Timer Flag 1
      ACALL DLOOP          // Calls subroutine DLOOP for displaying the count
      SJMP MAIN            // jumps back to the main loop
DLOOP: MOV R5,#252D
BACK1: MOV A,TL1           // loads the current count to the accumulator
       MOV B,#4D           // loads register B with 4D
       MUL AB              // Multiplies the TL1 count with 4
       MOV B,#100D         // loads register B with 100D
       DIV AB              // isolates first digit of the count
       SETB P1.0           // display driver transistor Q1 ON
       ACALL DISPLAY       // converts 1st digit to 7seg pattern
       MOV P0,A            // puts the pattern to port 0
       ACALL DELAY
       ACALL DELAY
       MOV A,B
       MOV B,#10D
       DIV AB              // isolates the second digit of the count
       CLR P1.0            // display driver transistor Q1 OFF
       SETB P1.1           // display driver transistor Q2 ON
       ACALL DISPLAY       // converts the 2nd digit to 7seg pattern
       MOV P0,A
       ACALL DELAY
       ACALL DELAY
       MOV A,B             // moves the last digit of the count to accumulator
       CLR P1.1            // display driver transistor Q2 OFF
       SETB P1.2           // display driver transistor Q3 ON
       ACALL DISPLAY       // converts 3rd digit to 7seg pattern
       MOV P0,A            // puts the pattern to port 0
       ACALL DELAY         // calls 1ms delay
       ACALL DELAY
       CLR P1.2
       DJNZ R5,BACK1       // repeats the subroutine DLOOP 100 times
       MOV P0,#11111111B
       RET

DELAY: MOV R7,#250D        // 1ms delay
 DEL1: DJNZ R7,DEL1
       RET

DISPLAY: MOVC A,@A+DPTR    // gets 7seg digit drive pattern for current value in A
         CPL A
         RET
LUT: DB 3FH                // LUT starts here
     DB 06H
     DB 5BH
     DB 4FH
     DB 66H
     DB 6DH
     DB 7DH
     DB 07H
     DB 7FH
     DB 6FH
END

http://ctoassembly.com

尝试在此处执行您的代码。 只需将它复制到主函数中,在while循环之前定义ab变量,就可以了。

您可以通过大量的解释了解如何将代码编译为汇编代码,然后您可以在假设的 CPU 中执行汇编代码。

虽然这是编译器的任务,但如果你想让你的手变脏,那么看看Godbolt

这是一款出色的编译器资源管理器工具,可让您将 C/C++ 代码逐行转换为汇编代码。

如果您是初学者并且想知道“C 程序如何转换为程序集?” 然后我在这里写了一篇详细的文章。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM