Author Topic: Translating C program to NASM  (Read 19047 times)

Offline GreXRa

  • Jr. Member
  • *
  • Posts: 4
Translating C program to NASM
« on: May 14, 2018, 08:33:34 PM »
Hello!

I am wondering how I can translate my program into NASM from C.
This is the original code:

Code: [Select]
#include <stdio.h>
#include <string.h>
 
int main()
{

   char lause[100];
   printf("Kirjuta s6na v6i lause mida tagurpidi keerata\n");
   gets(lause);
 
   strrev(lause);
 
   printf("\nTagurpidi kirjutatult on nii: \n%s\n", lause);
   
   system("pause");
   return 0;
}

Thanks for your help! :)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Translating C program to NASM
« Reply #1 on: May 14, 2018, 10:17:13 PM »
Hi GreXRa,
Welcome to the Forum.

A C compiler will translate your code to assembly language - but probably not Nasm syntax. I get the impression you want to do this "by hand" anyway. C is a "portable"language. A C compiler for Windows will produce code suitable for Windows, a Linux compiler for Linux, etc. They are not the same. So first you'll need to know what OS you want this for...

An exception would be "strrev()". That would be the same for any OS. You'll want a 16- or 32- or 64- bit register for the address of the string, but an 8-bit register for the character(s). I'm assuming ascii text.

You probably know that "gets()" is considered "dangerous" and shouldn't be used. The API you'll be using for your OS will want to know the size if the buffer anyway, so this probably won't be an issue.

"system("pause")" can probably be translated to "wait for a key". How "literal" a translation do you need?

Take a shot at this and show us where you get stuck, and we'll try to help you out from there.

Best,
Frank


Offline GreXRa

  • Jr. Member
  • *
  • Posts: 4
Re: Translating C program to NASM
« Reply #2 on: May 15, 2018, 08:03:22 AM »
Hey Frank!

Thanks for your answer. To be honest, I have no idea about assembly language. I was hoping that someone might translate this short code for me so I can understand what translates to what and how it is done. The Assembly needs to be in WINDOWS 32-bit.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Translating C program to NASM
« Reply #3 on: May 16, 2018, 03:50:50 AM »
I was afraid you were going to ask for Windows. I have no experience with Windows. In theory, I "ought" to be able to do a literal translation of your C code. I don't think you'd find it much use. It just calls "printf" (Windows calls it "_printf")  and hides all the interesting details. In fact, I have a version of it that sorta works for Linux. In theory, it ought to work for Windows. A 64-bit version wouldn't. It does not work well. I'm not happy with it at all. Now... I can't even copy it to here. Not a Forum problem. My old computer crashed and I'm too old or too dumb or both to figure out how  to run the new one. I just can't do this stuff anymore. I will attempt to attach it. Under the best of circumstances, I don't think it'll be much use to you. I hope you can find someone else to help you.

Best,
Frank



Offline dreamCoder

  • Full Member
  • **
  • Posts: 107
Re: Translating C program to NASM
« Reply #4 on: May 16, 2018, 10:58:55 AM »
Hey Frank!

Thanks for your answer. To be honest, I have no idea about assembly language. I was hoping that someone might translate this short code for me so I can understand what translates to what and how it is done. The Assembly needs to be in WINDOWS 32-bit.

Here's an alternative for Win32. Not that far off from Frank's.

Code: [Select]
;-----------------------------------
; Link with: gcc -m32 file.obj -o file.exe
;-----------------------------------
        extern _printf
        extern _strrev
        extern _gets
        global _WinMain@16
        ;extern _getch

        section .bss
lause:  resb 100

        section .data
msg1:   db "Kirjuta s6na v6i lause mida tagurpidi keerata",0ah,0
msg2:   db 0ah,"Tagurpidi kirjutatult on nii:",0ah,"%s",0ah,0

        section .text
_WinMain@16:
        push    ebp
        mov     ebp,esp

        push    msg1
        call    _printf
        add     esp,4

        push    lause
        call    _gets
        add     esp,4

        push    lause
        call    _strrev
        add     esp,4

        push    lause
        push    msg2
        call    _printf
        add     esp,8

        ;call    _getch
        xor     eax,eax
        leave
        ret

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Translating C program to NASM
« Reply #5 on: May 16, 2018, 04:46:37 PM »
Thanks dreamCoder!

Best,
Frank


Offline GreXRa

  • Jr. Member
  • *
  • Posts: 4
Re: Translating C program to NASM
« Reply #6 on: May 17, 2018, 08:01:26 PM »
Thank you Dreamcoder!

I only have one more question. If you enter the sentence it needs to reverse, it just finishes the program. There is no "Press any key to continue". So you cannot see the answer. How to solve that?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Translating C program to NASM
« Reply #7 on: May 17, 2018, 08:42:27 PM »
That's interesting. DreamCoder uses "_getch", but comments it out. My version, in Linux - same as your original C - uses "system("pause")". On Linux, "system()" works, but complains that it can't find "pause"! Try both, and if neither works, try to get yourself into a more permanent command prompt. "command" used to do it (long ago) - maybe "cmd" these days?

Best,
Frank



Offline GreXRa

  • Jr. Member
  • *
  • Posts: 4
Re: Translating C program to NASM
« Reply #8 on: May 20, 2018, 05:25:20 PM »
Hello again!

Thank you for your help thus far but I had to change the code a bit and cannot still figure out how to translate it to assembly. The first code you translated made sense but now I have some while cycles that i dont know how to translate.
Code: [Select]
#include <stdio.h>
#include <string.h>
#include <conio.h>
 
int main()
{

   char lause[100];
   int i=0, j, temp;
   printf("Kirjuta s6na v6i lause mida tagurpidi keerata\n");
   scanf("%s", lause);

   i = 0;
   j = strlen(lause) - 1;
   
   while (i<j)
   {
   temp = lause[i];
   lause[i] = lause[j];
   lause[j] = temp;

   i++;
   j--;

   }
 
   printf("\nTagurpidi kirjutatult on nii: \n%s\n", lause);
   
   system("pause");
   return 0;
}

How to do this?

Offline dreamCoder

  • Full Member
  • **
  • Posts: 107
Re: Translating C program to NASM
« Reply #9 on: May 20, 2018, 10:49:47 PM »
@GrexRa

Show what assembly codes you've attempted so far, then we'll help you where you need helping. Can't afford to spoonfeed you without you showing any serious attempts to learn.