News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

A Simple Convert C to Assembly Language

Started by dottywine, October 27, 2008, 06:01:04 AM

Previous topic - Next topic

dottywine

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


hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

donkey

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).
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

dottywine

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?

Vortex

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

Mark Jones

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.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

dsouza123

dottywine,

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

The .data section is already OK

Vortex


PauloH

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

GregL

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


dsouza123

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

dsouza123

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.

GregL

QuoteSo dw is the short form for WORD.

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


GregL

dsouza123,

Your code needs a correction.  :bg


movzx ecx, count



hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php