Thread: Calling a C function in assembly language

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    Exclamation Calling a C function in assembly language

    Hey guys...

    I am currently writing two programs, a transmitter and a receiver, which sends and receives a file between two computers over the COM port, the programs do work, however it's the encryption/cipher I am stuck on, I need to call a C function in assembler so that the character being read from the file is "encrypted".

    Also... if the code below doesn't make sense... the character from the file is converted to ASCII format, then the value is incremented by 10, it is then converted back to character value.

    Here's the code:

    C:
    Code:
    // Start of cipher algorithm //
    char cipher(char a){
    
    
         int b = a;
         int i = 0;
    
    
         // Loop to increment the ascii equivilent of the character by 10 //
         for(i = 0;i<10;i++){
             // Once the integer reaches the end of the ascii table, loop it back around to the start //
             if (b == 128){
                 b = 32;
             }else{
                 b++;
             }
         }
    
    
         char o=b;
    
    
         return o;
    
    
    }
    Assembly:

    Code:
    // while (not EOF) {
    top_of_loop:
    
    
            //        Read char from file
    
    
            mov eax, DWORD PTR fileptr        ; load the file/port ptr
            push eax                        ; push the file/port ptr onto the stack
            call DWORD PTR fgetc            ; call fgetc()
            add esp,4                        ; remove the file/port ptr from the stack
            mov chr, al                        ; char is returned in a
            cmp al, EOF
            je end_of_loop
    
    
            //        Write char to COM1
    
    
            mov eax, DWORD PTR portptr        ; load the file/port ptr
            push eax                        ; push the file/port ptr onto the stack
            mov al, chr                        ; load the char to be output
            push eax                        ; push char onto the stack (needs to be tested)
            call DWORD PTR fputc            ; call fputc()
            add esp, 8                        ; remove both parameters from the stack
            //    }
    
    
            jmp top_of_loop
    That assembly code is only a selection, but I believe the function needs to be called in there, as it's reading from the file and then writes it to the COM1 port.

    Regards,
    Chris
    Last edited by Chris Ambrose; 11-26-2012 at 03:47 AM. Reason: Clarification of C function

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Given the nature of that assembly code (obviously output from a C or C++ compiler) it would probably be easier to find the C/C++ source from which it was produced, modify that, and recompile.

    Why aren't you doing that?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Not sure what you mean, would it be possible for you to give me an example? Also, I'm using Visual Studio to produce this, for a Uni assignment

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That assembler code involves a couple of simple loops calling fgetc() - a function in the standard C library. No self-respecting assembler programmer would write such code, because it is not worth the time. One possible inference is that it has been produced by a C compiler (or, maybe, a C++ compiler) from some C source.

    Why aren't you modifying that C source file, and recompiling?? If you don't have access to it, why not? Why are you trying to modify assembler rather than C, given that the latter is much easier and less likely to result in curly questions in forums like this?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    I see your point, alas I am new to C programming and assembler, and so I believe the University believe it should break us in gently by giving us such assembler code (note the assembler was written mostly for us, we just have to put it all together along with a type of encryption i.e. XOR, block encryption etc.). As a 7 year long PHP programmer functions like this have obviously come out as a habit. As I said before, I am new to assembler programming so it seems old habits die hard.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    So I asked my lecturer... and he simply said, in assembler do "add al, 10" ... exactly what my C code does... oh how stupid I feel. Going to have to write something a... little more complex.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling assembly from c
    By Kuluzuva in forum C Programming
    Replies: 9
    Last Post: 05-16-2011, 03:54 AM
  2. Calling Assembly Language programs from C++ program
    By EonsNearby in forum C++ Programming
    Replies: 6
    Last Post: 04-23-2011, 04:58 AM
  3. Calling an Assembly Function
    By piommi in forum C Programming
    Replies: 4
    Last Post: 12-07-2007, 03:29 AM
  4. Calling C++ from assembly?
    By Chris361 in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2003, 06:17 AM
  5. help in assembly language
    By ema in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-14-2002, 02:22 AM

Tags for this Thread