Welcome to Dream.In.Code
Become an Expert!

Join 149,613 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,846 people online right now. Registration is fast and FREE... Join Now!




Conversion of C to Assembly language

 
Reply to this topicStart new topic

Conversion of C to Assembly language

eudos
17 Sep, 2007 - 09:08 AM
Post #1

D.I.C Head
**

Joined: 23 Apr, 2006
Posts: 58


My Contributions
Hi everyone,

I am trying to hand compile a C code to assembly language.

im having trouble to code only this part of my C code

CODE


while ((dividend > divisor) && !(divisor & 0x40000000 )){
     blaa..blaa

}
    



here is the assembly language code i have written
dividend = a1
divisor = a2

CODE


cmp    a1, a2                      ;comparing the value in register a1 and a2
blt       exit_loop                  ;if a1 <  a2 exit the while loop
and     a2, #0x40000000      ;bitwise AND the value in a2 with 0x40000000
(?????????)                         ;the question is how to translate "!" in assembly language




hope anybody who read this understand my problem. Can anybody plz help me on this.









User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Conversion Of C To Assembly Language
17 Sep, 2007 - 09:14 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,138



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
I would use jump if equal / jump if not equal.

http://209.85.165.104/search?q=cache:6zcb-...;cd=1&gl=us
check out page 8.
User is online!Profile CardPM
+Quote Post

born2c0de
RE: Conversion Of C To Assembly Language
17 Sep, 2007 - 10:14 AM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 4,026



Thanked: 38 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
This && !(divisor & 0x40000000 ) part of the condition can be greatly optimized by using the TEST instruction like this.
Even the comparison can be reduced. Have a look at how I am comparing(testing) only 1 byte instead of all 4 bytes.
(Works only for 32-bit ASM)

CODE
jmp short LOOP_BEGIN

LOOP_BODY:
                 BLAH...BLAH
LOOP_BEGIN:
                 mov     REGISTER, [dividend]
                 cmp     REGISTER, [divisor]
                 jle     short END_LOOP
; Have a look at the test instruction.            
                 test    byte ptr [divisor+3], 40h
                 jz      short LOOP_BODY
END_LOOP:

User is offlineProfile CardPM
+Quote Post

eudos
RE: Conversion Of C To Assembly Language
17 Sep, 2007 - 01:34 PM
Post #4

D.I.C Head
**

Joined: 23 Apr, 2006
Posts: 58


My Contributions
Thanks guys for the support,

I am still trying to figure out how to write the while loop in assembly language.

CODE

while ((dividend > divisor) && !(divisor & 0x40000000 )){
     blaa..blaa

}


the part NOT clear to me is the "!(divisor & 0x400000000)"

Can anybody tell me wats this part doing, i know its ANDing the divisor with 0x40h, (wats ! of the result)


CODE


        cmp    a1, a2                     ;comparing the value in register a1 and a2
        blt    L3                         ;if a1 <  a2 exit the while loop
        
        tst    a2, #0x40000000           ;AND the a2 with 0x40000000
        bne    L3



here i have used tst which changes the CPSR flage (updates the flage) But iam not clear of wat u to use "bne" or "beq"

born2c0de, thanks for the help, but i havent done (not studied yet) "jmp" so dont know wats happening in ur code.

Hope somebody will help me more

Thanks

This post has been edited by eudos: 17 Sep, 2007 - 01:35 PM
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Conversion Of C To Assembly Language
17 Sep, 2007 - 02:07 PM
Post #5

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,138



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(eudos @ 17 Sep, 2007 - 02:34 PM) *

born2c0de, thanks for the help, but i havent done (not studied yet) "jmp" so dont know wats happening in ur code.

Hope somebody will help me more

Thanks

I posted a link that describes in detail the jump (jmp) command. It's on page 8.
User is online!Profile CardPM
+Quote Post

hyperionza
RE: Conversion Of C To Assembly Language
17 Sep, 2007 - 02:31 PM
Post #6

New D.I.C Head
*

Joined: 15 Sep, 2007
Posts: 30


My Contributions
jmp is an unconditional jump
jz is 'jump if zero'
jle is 'jump if less than or eqaul to'

LOOP_BODY, LOOP_BEGIN and LOOP_END are labels, simply put they can be related to procedure calls, except these just jump straight to the next line of code, but unlike procedures these have no end point, unless the program is given another jump command it will just continue going sequencially through the rest of your code,

JMP and its cousins modify your program counter, line pointer or whatever you want to call it, to point to the next line of code it should execute. these jump points are given by the labels.

eg if you say
CODE
JMP 0h
, it will jump to the first line of your code

just like if your program looked like
CODE
0000 PROGRAM_START
0001 [some code]
.
.
.
0100 JMP PROGRAM_START


line 0100 will cause the program to jump back to line 0000

Dont take my word as gold, its been a year + since ive done this stuff, play with it, its the best way to learn.

have fun

hehe someone got there first...
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Conversion Of C To Assembly Language
17 Sep, 2007 - 06:55 PM
Post #7

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
Just a little FYI. You can generally get your C compiler to output Assembly. Especially if you are not using library function (which just come out as calls in the assembly code).

The other thing you can do is write the code, compile and then use a debugger or disassembler to view the compiled code. -- I learned a GREAT DEAL of assembly this way. I also learned a great deal about C this way.
User is offlineProfile CardPM
+Quote Post

eudos
RE: Conversion Of C To Assembly Language
18 Sep, 2007 - 12:37 AM
Post #8

D.I.C Head
**

Joined: 23 Apr, 2006
Posts: 58


My Contributions
thanks guys. i have fixed the problem now..here is the code i used.

CODE


        cmp    a1, a2                     ;comparing the value in register a1 and a2
        blt    L3                              ;if a1 <  a2 exit the while loop
        
        tst    a2, #0x40000000           ;AND the a2 with 0x40000000
        bne    L3                                ;if CPSR is not equal to zero, exits the while loop



i use a debugger to fix the code, and also i am using the ARM architecture as a target mem.

biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:24AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month