Репликация C программы в ассемблерном коде - PullRequest
0 голосов
/ 03 мая 2018

Мне нужно скопировать простой 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 голосов
/ 04 мая 2018

Закомментировав раздел 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...