The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: dottywine on October 27, 2008, 06:01:04 AM

Title: A Simple Convert C to Assembly Language
Post by: dottywine on October 27, 2008, 06:01:04 AM
I know there are many ways to do it. If you are bored and want a little puzzle, please see if you can convert this C into Assembly (I don't think its hard). If you do not want to do this, please answer the question I have at the bottom (near the bottom). Thankyou!

int count = 0;
while (count < arraySize) {
  if (array[count]<100)
    sum1 = sum1 + array[count];
  else
    sum2 = sum2 + array[count];
  count++;
}
.data
count dw 0
array dw 50, 120, 100, 40
arraySize = ($-array)/(TYPE array) // can someone explain this line? Especially the dollar sign.
sum1 dw 0
sum2 dw 0

Title: Re: A Simple Convert C to Assembly Language
Post by: hutch-- on October 27, 2008, 06:30:55 AM
If you wish to make requests, try rent a coder, I am sure someone will cater to what you need for a price. Try the assembler listing from a C compiler, it will do what yuu require but you have to learn how to do that as well.
Title: Re: A Simple Convert C to Assembly Language
Post by: donkey on October 27, 2008, 01:14:43 PM
Quote from: dottywine on October 27, 2008, 06:01:04 AM
I know there are many ways to do it. If you are bored and want a little puzzle, please see if you can convert this C into Assembly (I don't think its hard). If you do not want to do this, please answer the question I have at the bottom (near the bottom). Thankyou!

...
array dw 50, 120, 100, 40
arraySize = ($-array)/(TYPE array) // can someone explain this line? Especially the dollar sign.
...

Well, I'm not going to translate for you, its a simple problem and you should be able to figure it out  yourself with a bit of research however the $ is the current location counter so current location minus the start of the array will yeild the array size in bytes (in this case 8).
Title: Re: A Simple Convert C to Assembly Language
Post by: dottywine on October 27, 2008, 04:15:59 PM
Hutch, I have been trying to find an assembler coder for a while and i can't find one, so if you know of someone who is willing to, please send me a message. Thanks.

Also, I do not have a C compiler. I'm using a mac. If you know of a C or C++ compiler, please, please send me a message. I would appreciate that, as well.

Oh, and I know you question my diligence... I've been googling for an assembly coder for a while and for a FREE C++ compiler. Usually I use netbeans, but the C++ extension doesn't work and I don't plan on using Java again now that I am learning C++ so I uninstalled it. So if you have some knowledge, if you could share with me, I'd appreciate it. Thankyou.

donkey, so... the current location of arraySize minus the location of the beginning of the array?
Title: Re: A Simple Convert C to Assembly Language
Post by: Vortex on October 27, 2008, 06:29:16 PM
Hi dottywine,

Creating an assembly listing from your C code is easy. Here is how you do it with VC++ 2005 Express edition :

cl /c /Fa sourcefile.c
Title: Re: A Simple Convert C to Assembly Language
Post by: Mark Jones on October 27, 2008, 08:22:12 PM
Unfortunately, Vortex's suggestion of using VC++ 2008 (Microsoft Visual C++) will only run on Windows.

Dotty, the NetBeans C++ add-in package is for NetBeans itself to understand C++ code, and is not a stand-alone C++ compiler. Google for "C++ Compiler MAC" or similar to find a C++ compiler that works on your platform. (THEN NetBeans with the C++ add-in should work.) :U

As far as assembly listings go, usually this is some kind of option or setting in the C++ compiler. First there should be an option to build in "release" mode and "debug" mode somewhere. Choose the latter, this may give more information. Then turn on assembly listing output. This should spit out an .ASM file or similar. Study this file to see what the compiler is doing.

If you have a PC, you could also install a debugger such as OllyDbg, and use that to step-through each instruction of your compiled executable file. Search here for "OllyDbg step" or similar to find some examples how to do it.
Title: Re: A Simple Convert C to Assembly Language
Post by: dsouza123 on October 27, 2008, 08:37:03 PM
dottywine,

If you tweak the C code into high level MASM syntax,
you will be nearly there.

The .data section is already OK
Title: Re: A Simple Convert C to Assembly Language
Post by: Vortex on October 27, 2008, 08:46:33 PM
What about running Vmware fusion on Mac?
Title: Re: A Simple Convert C to Assembly Language
Post by: PauloH on October 28, 2008, 02:23:50 AM
Ms. dottywine,

To talk about this line we need to talk about the .data section:
arraySize = ($-array)/(TYPE array).

You are wondering: Why this works? To understand it you must look the line before it:
array dw 50, 120, 100, 40.

So, your .data section is like this:

.data
some stuff here...
....
array dw 50, 120, 100, 40
arraySize = ($-array)/(TYPE array)
......

It works because the way that is written*!!! See, the $ sign means the "current location at memory", or, if you prefer, you can think of it as the current line pointer.
When you defined array, you give a memory location the name "array". The current location ($)  at ($-array) means: this location (or this address) minus (-) the (start) location of array. What is the answer? Think about the array definition: array is defined as a set of 4 words. A word is 2 bytes lenght (and a dword is 4 bytes lenght). Now,

What does it mean? It means: arraySize = ($ - array) / (TYPE array) = 8 / 2 = 4.

from the Masm programmer's manual, chapter 5:

"The LENGTHOF operator returns the number of elements in the array. The SIZEOF operator returns the number of bytes used by the initializers in the array definition. TYPE returns the size of the elements of the array. "

Well, you could get the same result with this:
arraySize = SIZEOF(array) / TYPE (array)

or:
arraySize = LENGTHOF (array)


* If you write:
array dw 50, 120, 100, 40
anydword dd 0, 0,0,0
otherword dw 34,20,30
lastbyte db 0,20,00
arraySize = ($-array)/(TYPE array)

You will change your results on a very bad way!  :bdg

I hope this can help you.
PauloH
Title: Re: A Simple Convert C to Assembly Language
Post by: GregL on October 28, 2008, 02:39:47 AM
I wanted a little puzzle.


.DATA

    count DWORD 0
    sum1  DWORD 0
    sum2  DWORD 0
    array DWORD 50, 120, 100, 40
    arraySize EQU $-array

.CODE

  start:

    .WHILE count < arraySize
        mov edx, count
        mov ecx, array[edx]
        .IF ecx < 100
            mov eax, sum1
            add eax, ecx
            mov sum1, eax
        .ELSE
            mov eax, sum2
            add eax, ecx
            mov sum2, eax
        .ENDIF
        add count, SIZEOF DWORD
    .ENDW

Title: Re: A Simple Convert C to Assembly Language
Post by: dsouza123 on October 28, 2008, 08:30:24 PM
Now that the puzzle is out of the box.
This uses the word size variables of the original post.


.data
   count dw 0
   array dw 50,120,100,40
   arraysize = ($-array)/(TYPE array)
   sum1  dw 0
   sum2  dw 0

.code
start:
   mov ecx, count
   .while (ecx < arraysize)
      .if (array[ecx*2] < 100)
         mov ax, array[ecx*2]
         add sum1, ax
      .else
         mov ax, array[ecx*2]
         add sum2, ax
      .endif
      inc ecx
   .endw

or a little smaller version

   mov ecx, count
   .while (ecx < arraysize)
      mov ax, array[ecx*2]
      .if (ax < 100)
         add sum1, ax
      .else
         add sum2, ax
      .endif
      inc ecx
   .endw
Title: Re: A Simple Convert C to Assembly Language
Post by: dsouza123 on October 28, 2008, 08:44:21 PM
Remember

db == data byte == BYTE   8 bits
dw == data word == WORD   16 bits
dd == data double word == DWORD   32 bits

So dw is the short form for WORD.
Title: Re: A Simple Convert C to Assembly Language
Post by: GregL on October 28, 2008, 11:22:08 PM
QuoteSo dw is the short form for WORD.

Yes, I noticed that, but I decided to go with dwords. It just seemed more 'correct'.

Title: Re: A Simple Convert C to Assembly Language
Post by: GregL on October 28, 2008, 11:52:03 PM
dsouza123,

Your code needs a correction.  :bg


movzx ecx, count


Title: Re: A Simple Convert C to Assembly Language
Post by: hutch-- on October 30, 2008, 05:42:32 AM
Dotty,

Quote
Hutch, I have been trying to find an assembler coder for a while and i can't find one, so if you know of someone who is willing to, please send me a message. Thanks.
Also, I do not have a C compiler. I'm using a mac. If you know of a C or C++ compiler, please, please send me a message. I would appreciate that, as well.

You have plain wasted our time here, you come in asking questions that you don't have a clue about with tools you do not even barely understand and then tell us you are using a MAC. I am not going to waste my time doing anything for you including searching for a programmer. If you really and truly want to write software for a MAC, go back to your MAC dealer and ask them or ask Apple to help you as you will get little else anywhere else.