# Sonic and Sega Retro Message Board: How to translate basic 68k assembly to basic c code - Sonic and Sega Retro Message Board

Page 1 of 1

## How to translate basic 68k assembly to basic c code

### #1Chris Pancake    Posted 22 December 2017 - 07:31 PM

• Posts: 7
• Joined: 16-December 17
• Gender:Not Telling
• Project:Sonic Union
This is my first topic!
Unfortunately I was unable to find the c equivalent to the bset, bclr, bchg, bvc and bvs.. No, it was just laziness.
If you want to know how to do ROL (bit rotate left) and ROR (bit rotate right), look: there.

Variable width
Spoiler

Local variables
Spoiler

Conditions
Spoiler

Addiction and subtraction
Spoiler

The clear, swap and exchange instruction
Spoiler

Bitwise operators
Spoiler

The neg and ext
Spoiler

Multiplication and division
Spoiler

Branch on sign
Spoiler

This post has been edited by Chris Pancake: 31 March 2018 - 08:15 AM

### #2MainMemory  Posted 23 December 2017 - 01:55 AM

• Every day's the same old thing... Same place, different day...
• Posts: 4178
• Joined: 14-August 09
• Gender:Not Telling
• Project:SonLVL
• Wiki edits:1,339
Your "Addition and Subtraction" section uses d1 as a pointer, which is not permitted on 68000. Aside from that, a lot of this is just common sense stuff that you'd figure out if you know C and 68000 ASM.
The equivalent to bset #X,d0 is d0 |= 1 << X;
The equivalent to bclr #X,d0 is d0 &= ~(1 << X);
The equivalent to bchg #X,d0 is d0 ^= 1 << X;

### #3Chris Pancake  Posted 23 December 2017 - 08:32 AM

• Posts: 7
• Joined: 16-December 17
• Gender:Not Telling
• Project:Sonic Union

MainMemory, on 23 December 2017 - 01:55 AM, said:

Your "Addition and Subtraction" section uses d1 as a pointer, which is not permitted on 68000. Aside from that, a lot of this is just common sense stuff that you'd figure out if you know C and 68000 ASM.
The equivalent to bset #X,d0 is d0 |= 1 << X;
The equivalent to bclr #X,d0 is d0 &= ~(1 << X);
The equivalent to bchg #X,d0 is d0 ^= 1 << X;

To be honest, I didn't have so much practice with the 68k, already fixed that.

### #4flamewing  Posted 27 December 2017 - 06:51 AM

• Emerald Hunter
• Posts: 1137
• Joined: 11-October 10
• Gender:Male
• Location:ðŸ‡«ðŸ‡· France
• Project:Sonic Classic Heroes; Sonic 2 Special Stage Editor; Sonic 3&K Heroes (on hold)
• Wiki edits:12

Chris Pancake, on 22 December 2017 - 07:31 PM, said:

register int d0 = hello;
register int d1 = world;

For what is worth, "register" keyword is mostly ignored by C and C++ compilers and, since C++11, it has the same semantics as whitespace in C++. C, even C11, still pretends it does something but, in practice, it does not.

### #5Chris Pancake  Posted 03 January 2018 - 05:49 PM

• Posts: 7
• Joined: 16-December 17
• Gender:Not Telling
• Project:Sonic Union

flamewing, on 27 December 2017 - 06:51 AM, said:

Chris Pancake, on 22 December 2017 - 07:31 PM, said:

register int d0 = hello;
register int d1 = world;

For what is worth, "register" keyword is mostly ignored by C and C++ compilers and, since C++11, it has the same semantics as whitespace in C++. C, even C11, still pretends it does something but, in practice, it does not.

With optimization on of course (MSVC /Ox and GCC -Ox for example), that's why I said:

Quote

This keyword is not needed when this is a release build though. (which normally has the O2 build option).
I also changed the name of this topic because this guide is really basic.

### #6lil-g-gamegenuis  Posted 06 January 2018 - 07:19 PM

• GO! GO! GO! GO! GO! GO!
• Posts: 24
• Joined: 03-April 12
• Gender:Male
• Location:olathe, ks
• Project:Practicing to make better SMPS
Actually even when optimization is off, it's still ignored. Compilers nowadays only treat it as a keyword with no meaning. Its about as useful as the auto modifier (Note: Not the auto type)

### #7Revival  Posted 17 January 2018 - 12:04 AM

• The AppleTalk Network System
• Posts: 200
• Joined: 08-July 07
• Gender:Male
• Location:The British Republic (< 3)
• Wiki edits:34
This certainly seems correct, but this is something that could be done by an automatic translator. Aside from folding some lines into one-another the result of that would almost exactly match the original assembly on a line-by-line basis. It would be far from idiomatic C++.

If a direct translation of a whole program from 68k ASM to C++ were intended, it would probably be better to make drastic changes - abandon manipulation of global variables representing the registers, for example, and instead make the subroutines pass data as actual arguments. The modern optimising C++ compiler could probably match the performance of the original assembly.

### #8flamewing  Posted 17 January 2018 - 09:25 AM

• Emerald Hunter
• Posts: 1137
• Joined: 11-October 10
• Gender:Male
• Location:ðŸ‡«ðŸ‡· France
• Project:Sonic Classic Heroes; Sonic 2 Special Stage Editor; Sonic 3&K Heroes (on hold)
• Wiki edits:12

Revival, on 17 January 2018 - 12:04 AM, said:

The modern optimising C++ compiler could probably match the performance of the original assembly.

They cannot, unless you are talking about really bad assembly; mostly because the modern C++ compilers which support 68k family at all are more oriented towards later models of the 68k family (which have pipelines), as well as using an ABI which makes heavy use of the stack.

Page 1 of 1