Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Porting C source in unix in windows

Status
Not open for further replies.

Neha Punia

Newbie level 1
Joined
Mar 9, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,288
How i make C program written in bash shell in unix portable to windows operating system .
I dont want to use the tool like cygwin for this but want to do some modification in header file & declaration where ever needed.
Also i want the program run on both windows as well as linux.
Suggest any solution...
along with the sample code(if u can).
 

How i make C program written in bash shell in unix portable to windows operating system .

C programs written in Bash shell???? what exactly you mean???

I dont want to use the tool like cygwin for this but want to do some modification in header file & declaration where ever needed.

Yes perfectly it is doable. But it is very tedious.
Also i want the program run on both windows as well as linux.
Suggest any solution...
along with the sample code(if u can).

Do you mean to use the compiled binary will run on both Windows and Linux???
It is next to Impossible.

If you want the code can be compiled on both the platform then it is ok.

BUT there is a **** in the ELF header and I have seen an assembly code written in NASM syntax , after assembled and linked can run on dos prompt and Linux CLI.

---------- Post added at 05:45 ---------- Previous post was at 05:39 ----------

Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Example of program that runs under  ;;
;;Linux and DOS (sort of)             ;;
;;Note :- as AX is not guaranteed to  ;;
;;be 0 on entry of .com   executable  ;;
;;AFAIK, use at own risk.             ;;
;;19/01/2008                          ;;
;;Adapted for NASM V2.01              ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

use16
     org    08048000h              ;Seems to be the norm for ELF ?
ComOff equ $$-100h                 ;Org 100h displacement
;----------------------------------------------------------
ehdr:                              ;Elf header
     db     0x7F, 'ELF'            ;Elf ident, might jump to Com if flag is ok (7F=JG)
     db     1                      ;32 bit architecture
     db     1                      ;little Endian
     db     1                      ;ELF version
Pad9:                              ;Start of padding, 9 bytes
     db     1                      ;Padding, 9 bytes. Keep it as add [bx+di],ax
                                   ;rather than adding [bx+si] if 0
                                   ;the less that is changed,
                                   ;the less to go wrong, perhaps...
     inc    sp                     ;Undo what was done by 7fELF111, don't worry about SI
     sub    [bx+di],ax             ;ax seems to be 0 on entry but, just in case
     sub    [bx+di],ax             ;if we got this far we should be ok
     jmp    Com
     times 9-($-Pad9) db 0         ;Rest of the padding, if any!
     dw     2                      ;Executable
     dw     3                      ;Intel 386+
     dd     1                      ;Version
     dd     start                  ;Program start
     dd     phdr - $$              ;Program header offset
     dd     0                      ;Section header offset
     dd     0                      ;Flags
     dw     ehdrsize               ;Elf header size
     dw     phdrsize               ;Program header size
     dw     1                      ;Number of program header entries
     dw     0                      ;Section header size
     dw     0                      ;Number of section header entries
     dw     0                      ;String table (undefined)
ehdrsize equ $-ehdr
;----------------------------------------------------------
     times  71-ehdrsize  db 0      ;Could use this wasted space for something...
Com:                               ;Where '7fE' JG +47h jumps too
     mov    eax,'DOS'+0d000000h    ;Change 'Linux',0ah to 'DOS',0dh,0ah
     mov    bx, 0ah                ;
     mov    [OrNot-ComOff],eax     ;
     mov    [OrNot+4-ComOff],bx    ;Messy offsets !
     mov    si,msg-ComOff
     call   dword OutputIt         ;Print message
     mov    ax,4c00h               ;Exit
     int    21h
     int    20h                    ;Just in case
;----------------------------------------------------------
align 16                           ;Keep Linux happy :)
phdr:                              ;Elf program header
     dd     1                      ;Load segment
     dd     0                      ;offset
     dd     $$                     ;Virtual address
     dd     $$                     ;Physical address
     dd     filesize               ;File image size
     dd     filesize               ;Memory image size
     dd     5                      ;Executable / readable x_r
     dd     1000h                  ;Alignment
phdrsize equ $-phdr
;----------------------------------------------------------
use32
start:
     mov    eax,4                  ;Write msg to console
     mov    ebx,1                  ;
     mov    ecx,msg                ;
     mov    edx,msg_size           ;
     int    80h                    ;

     mov    eax,1                  ;Exit
     xor    ebx,ebx                ;
     int    80h                    ;
;----------------------------------------------------------
msg  db     'This program has been run from some sort of '
OrNot:
     db     'Linux',0ah
msg_size equ $-msg
;----------------------------------------------------------
use16
OutputIt:                          ;Print text string from si
     mov    ah,0eh                 ;could use int 21h and ah=2 instead with dl
     mov    bx,05h                 ;probably don't need this (graphics mode color)
@1:  lodsb
     or     al,al
     jz     @2                     ;End of string
     int    10h
     jmp    @1
@2:  ret

filesize equ $-$$

This is not my code. I got it from Internet I forgot the source I got it from.
I don't claim anything and don't be responsible for any thing. Use it if you wish.

What I can say is I tested it under Fedora-13 and free dos. it is working.
 

The answer to your question depends upon the size of your C code.
If it is a small C code (few hundred lines of codes) and doesn't use core OS functions, then porting it to Windows from Unix by modifying the header files etc. is easy otherwise think again why do you need to run it on windows without using cygwin.

On another note; could it be possible that you are looking at a shell script?
If it is a bash script; it is a shell script and you do need a bash shell or a similar one to run that with minimum fuss. OR understand the logic and implement in .bat file :)

The third possibility (which I think is what you wanted to say) that you are willing to modify the code but need compilation tools similar to those under Unix.
So if You are looking for a compiler (not gcc which runs on windows through cygwin or minGW) then you can use Visual C++. You can download the command line version of the Visual Studio compiler from Microsoft website free of charge. (But Visual Studio is a nice IDE if you haven't used it before).

Regarding running the code on both OS; you can do that (again it depends upon the size and nature of your code)
1) develop a little OS abstraction layer
2) conditional compilation
3) use MinGW
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top