在汇编代码中复制C程序

时间:2018-05-03 04:44:12

标签: assembly x86

我需要在人类编写的汇编代码中复制一个简单的C代码。在过去的一周里,我已尽力研究这一点,但对于我所学到的每件事,我似乎都没有接近完成这个目标。我已经以多种方式编写了代码,我认为应该可行。在某些版本的代码中,我的变量没有正确初始化,所以cmp jmp永远不会触发给我无限的输出行。在其他版本中,它会在没有输出的情况下崩溃,即使我改变的代码会在输出行之后发生。

%include "io.inc"
extern printf ; brings in the printf to be called for output
section .data
section .text
    jar DD 4 ; The jar variable is the primary output of the function during printf
    iar DD 0 ; The iar variable is a counter for a while loop which runs through 8 times
    message: db "num: %d" , 10, 0; will be pushed to stack to make the printf statement work
global CMAIN
CMAIN:
    inc dword[iar] ; incraments the iar variable by one.
    mov eax, [iar]; put the iar into eax register so it can be added to the jar variable
    add [jar], eax ;jar= jar + iar
    cmp dword[jar], 20; compare jar to 20 (jar>20)
    jl RE ; if jar is less than 20 skip the next step
    sub  dword[jar], 20; otherwise subtract 20 from j 
RE:  
    mov eax, [jar] ; move the jar variable to the eax register to be pushed to the stack
    push  eax ; push jar for printf
    push message ; push formating for printf
    call printf   ; print the primary output "num: [jar]"
    pop eax ; clear the stack 
    pop eax ; clear the stack   
    cmp dword[iar],8 ; Compare iar to 8 to see if iar has been incremented 8 times   
    jne CMAIN ; if i != 8 jump to cmain
pleaseKillMeNow:
    mov ah,0x4C  ;graceful exit
    int 0x21

此代码用于复制以下C代码

#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]){
    int iar;
    int jar;
    jar=4;
    iar=0;
    while(i<8){
        jar=jar+1;
        if(jar>20){
        jar=jar-20;
        }
        printf("%d",j);
        i++;
        j=j+1;
        i++;
        printf("%d",j);
    }
return j;
}

我很感激我可以去调试它的任何方向。我现在正在使用SASM来解决这个问题。谢谢

1 个答案:

答案 0 :(得分:0)

通过注释掉pleasekillmenow:部分,并将声明返回到.data部分,我得到的代码按预期工作

%include "io.inc"
extern printf ; brings in the printf to be called for output
section .data
    jar DD 4 ; The jar variable is the primary output of the function during printf
    iar DD 0 ; The iar variable is a counter for a while loop which runs through 8 times
    incs DD 1
section .text

    message: db "num: %d" , 10, 0; will be pushed to stack to make the printf statement work
global CMAIN
CMAIN:
    mov ebp, esp; for correct debugging
    mov eax, [iar]; put the iar into eax register so it can be added to the jar variable
    add [jar], eax ;jar= jar + iar
    cmp dword[jar], 20; compare jar to 20 (jar>20)
    jl RE ; if jar is less than 20 skip the next step
    sub  dword[jar], 20; otherwise subtract 20 from j 
RE:  
    mov eax, [jar] ; move the jar variable to the eax register to be pushed to the stack
    push  eax ; push jar for printf
    push message ; push formating for printf
    call printf   ; print the primary output "num: [jar]"
    pop eax ; clear the stack 
    pop eax ; clear the stack   
    mov eax, [incs]
    add [iar],eax
    cmp dword[iar],8 ; Compare iar to 8 to see if iar has been incremented 8 times   
    jl CMAIN ; if i != 8 jump to cmain
;pleaseKillMeNow:
   ; mov ah,0x4C  ;graceful exit
   ; int 0x21