4

Hello StackOverFlowers!

I'm extremely new to the whole linux thing and I'm using Cygwin to try to do the following..

Put a test file through a parser...

./parser < test.c > test.asm //Works and generates ASM code

Do this line as described in the tutorial which I'm not sure exactly what it does...

nasm -f elf test.asm //Works

And finally this line to create the exectuable(If I am understanding the tutorial correctly)

ld -s -o test test.o

I'm getting an error of ld: i386 architecture of input file test.o' is incompatible with i386:x86-64 output on this line. I know it's because I'm trying to go from a 32-bit to a 64-bit, but I can't figure out how to combat this. I've tried using elf_i386, but I get ld: unrecognised emulation mode: elf_i386. Supported emulations: i386pep i386pe.

Any and all suggestions are much appreciated!!

EDIT:

Test.asm file

section .data
numbers db "0123456789", 0xA
inputchar db 0
section .text
global F_124565444
F_124565444:
push    ebp
mov ebp, esp
sub esp, 4
mov dword [ebp-4], 0
jmp G2
G1:
add dword [ebp-4], 1
push edx
G2:
mov edx, 0
mov eax, [ebp+8]
mov ebx, 10
div ebx
mov [ebp+8], eax
cmp eax, 0
jnz G1
push edx
add dword [ebp-4], 1
jmp G3
G4:
sub dword [ebp-4], 1
pop edx
mov eax, 4
mov ebx, 1
mov ecx, numbers
add ecx, edx
mov edx, 1
int 80h
G3:
cmp dword [ebp-4], 0
jnz G4
mov eax, 4
mov ebx, 1
lea ecx, [numbers+10]
mov edx, 1
int 80h
leave
ret
global F_7362500
F_7362500:
push    ebp
mov ebp, esp
sub esp, 4
mov dword [ebp-4], 0
mov byte [inputchar], 0
jmp G6
G5:
mov dword eax, [ebp-4]
mov ebx, 10
mul ebx
xor ecx, ecx
mov byte cl, [inputchar]
sub ecx, 48
add eax, ecx
mov dword [ebp-4], eax
G6:
mov eax, 03h
mov ebx, 00h
mov ecx, inputchar
mov edx, 01h
int 80h
cmp byte [inputchar], 0ah
jne G5
mov dword eax, [ebp-4]
leave
ret
global F_28052
F_28052:
push    ebp
mov ebp, esp
push    dword [ebp+8]
push    dword 0
pop ebx
pop eax
cmp eax, ebx
sete    al
movzx   eax, al
push    dword eax
pop eax
cmp eax, 0
je  L1
push    dword [ebp+12]
pop eax
leave
ret
jmp L2
L1:
push    dword [ebp+8]
push    dword [ebp+12]
push    dword [ebp+12]
push    dword [ebp+8]
pop ebx
pop eax
mov edx, eax
sar edx, 31
idiv    ebx
push    dword eax
push    dword [ebp+8]
pop ebx
pop eax
imul    eax, ebx
push    dword eax
pop ebx
pop eax
sub eax, ebx
push    dword eax
call    F_28052
add esp, 8
push    dword eax
pop eax
leave
ret
L2:
leave
ret
global _start
_start:
push    ebp
mov ebp, esp
sub esp, 4
sub esp, 4
call    F_7362500
add esp, 0
push    dword eax
lea ebx, [ebp-4]
pop eax
mov dword [ebx], eax
call    F_7362500
add esp, 0
push    dword eax
lea ebx, [ebp-8]
pop eax
mov dword [ebx], eax
push    dword [ebp-4]
push    dword [ebp-8]
call    F_28052
add esp, 8
push    dword eax
call    F_124565444
add esp, 8
push    dword 0
pop eax
leave
mov ebx, eax
mov eax,1
int 80h
leave
mov ebx, eax
mov eax,1
int 80h
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Becca Bohem
  • 139
  • 1
  • 2
  • 8
  • It created the `test.exe` but when I try to run it by clicking on the exe in the folder it gives an error of `testing.exe has stopped working`, and when I try to run it through cygwin using ./testing.exe, it gives a Segmentation Fault. Any Ideas @MichaelPetch? – Becca Bohem May 03 '16 at 18:35
  • @MichaelPetch it has been added, thanks for your efforts by the way. I am so lost – Becca Bohem May 03 '16 at 18:37
  • I accidentally deleted my first suggestion, I'm reposting it: i don't believe the precompiled versions of _LD_ on Cygwin understands anything other than i386pep and i386pe object files. This might work by telling nasm to generate win32 object. `nasm -f win32 test.asm -o test.obj` then `ld -mi386pe -o test.exe test.obj` – Michael Petch May 03 '16 at 18:44
  • With that being said `int 0x80` will not work under Windows/Cygwin. It is a Linux interrupt, and that is likely causing segfaults. – Michael Petch May 03 '16 at 18:45
  • I'm not sure I understand, where is the `int 0x80` you speak of, and is there anyway of combating that as well? Secondly, at the end of the day I just want to make sure that the `test.asm` has valid code that runs in it, could I somehow avoid these problems all together? Sorry for all the questions, I'm just completely new to the whole linux thing, I'm a long way from my java home – Becca Bohem May 03 '16 at 18:51
  • In your code you have it coded as `int 80h` . `int 0x80` are the same thing in NASM. – Michael Petch May 03 '16 at 18:52
  • First off, Cygwin isn't Linux and that is the first problem. It seems like you have tried taking Linux assembler code in the hopes it works under Cygwin. `int 80h` is Linux, and won't work on Linux/Cygwin. I'd be tempted to do your output linking to the _C_ library and using things like `printf` to print to the console. – Michael Petch May 03 '16 at 18:54
  • I am running Cygwin under windows, how would I go about linking to the C libary? Could I simply replace the `int 80h` with something else? @MichaelPetch – Becca Bohem May 03 '16 at 19:06
  • First off linking against the _C_ library is the easy part, but you'll have to remove all your `int 80h` system calls and replace them with calls to things like `_scanf`, `_printf`, `_exit`. You'd have to change your program entry point to `_main`. Then you assemble and link with something like `nasm -f win32 test.asm -o test.obj` then link with `gcc test.obj -o test.exe` – Michael Petch May 03 '16 at 19:37
  • Converting this code to use the _C_ library and explaining that IMHO would be too broad of a question. There is an example of calling `_printf` in this [SO Answer](http://stackoverflow.com/a/1023600/3857942) - completely ignore (pretend it isn't in the answer) the code in the bottom snippet that start with `org 100h` - it doesn't apply. – Michael Petch May 03 '16 at 19:39
  • Could I, in theory, create a linux virtual machine, and then bypass all of these issues? @MichaelPetch – Becca Bohem May 03 '16 at 19:45
  • Cygwin isn't Linux. If you want to create Linux applications then yes, I'd highly recommend installing Linux into a Virtual machine. Cygwin development tools generate code that runs directly on Windows (not Linux). Probably you got all confused thinking that Linux and Cygwin were the same which is not the case. – Michael Petch May 03 '16 at 19:46
  • I thought cygwin was exactly linux, thank you so much for all your help! If you'd make an answer I'd gladly accept it! – Becca Bohem May 03 '16 at 19:54

1 Answers1

5

It appears from your question and comments that you may have been lead to believe that Cygwin was the same as Linux. That isn't the case. Cygwin is a unix-like posix compatible environment that runs natively on Windows:

Cygwin is a Unix-like environment and command-line interface for Microsoft Windows. Cygwin provides native integration of Windows-based applications, data, and other system resources with applications, software tools, and data of the Unix-like environment. Thus it is possible to launch Windows applications from the Cygwin environment, as well as to use Cygwin tools and applications within the Windows operating context.

In particular all the Linux specific code that uses int 80h system call will fail when run on Windows, as it doesn't apply to the Windows environment.

If you want to create code that runs on Linux, then you'll need to install Linux. You can do that by running Virtual Machine software on Windows and install a flavor of Linux into it (Ubuntu is the choice for a lot of people).

Michael Petch
  • 46,082
  • 8
  • 107
  • 198