Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C Code to Assembly Language

Status
Not open for further replies.

noobmaster

Newbie
Joined
Feb 28, 2021
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
15
Hi, I'm a complete beginner but how would I do about translating this to assembly?
f = 0;
for (i=0; i<w; i++) {
if (results >= 70)
x = x + 1;
else if(results >= 60)
y = y + 1;
else if(results >= 50)
z = z + 1;
else
f = f + 1;
}
 

Hi,

This is the job of a compiler.

The compiler at least needs to know
* the source code language
* the target controller (language, or instruction set)

Klaus
 
Hi,

This is the job of a compiler.

The compiler at least needs to know
* the source code language
* the target controller (language, or instruction set)

Klaus
Hi Klaus, thanks for your reply.
I'm currently needing to do this for a University piece of coursework and we're expected to just convert it ourselves, is there a way for this?
 

You can do this manually, although its tedious.

You have to understand the machine instruction code for the processor
you are using, its instruction naming conventions, and how they operate.
Not all instructions, basic load/store/test/add/sub/and......because your
C code for conversion

If this is just a part of the overall C code you can do inline assembly, consult
the compile and linker manuals on how to do this.

Basically you take this one step at a time

0) If inline ASM look into how to save / restore machine state
1) Declare variables / registers to use in ASM
2) Find instructions to do a jump to label (this is the FOR loop in C
3) Look at instruction to load / test / store (flags will be set cleared in
machine by some of these like add/ sub......)

If you have never done ASM before do a simple task, like store a value
into memory or register, then load it into ACCUM, and then write that
out to a port register. Use debugger to verify, or put, for example, 4 leds
on the port, bits 0, 2, 4, 6, and write out to port 0b'01010101' to light them
all up, then write the complement of the ACCUM to turn them off.

Then do a simple test of a value, if its greater than another value, light
the LEDs...

Simple tests/operations will make it easy to maintain your sanity when learning
machine language....:) Keep a programmers reference card next to you that shows
a summary of the instructions makes life easy. Note machine code instructions,
on the whole, are quite simple operations, add sub logical test jump .....so its
not rocket science to learn. You are not solving Maxwell's equations for boundary
layer conditions......in N dimensions....ugh.....


Watch a couple of youtube videos on ASM for starters for general principles....

Note you can always look at compiler code produced from your C, but be aware
that compilers can do certain optimization methods that may make the ASM
code a little more challenging to read than when you write it yourself as a newbie.
The file that shows you the compiler produced ASM code is typically named main.lst
and its ancillary list files.


Regards, Dana.
 

Attachments

  • QRC0001_UAL.pdf
    226.5 KB · Views: 229
Last edited:
Hi

I've written assembly code many times. You need to be familiar with the microcontroller especially with the instruction set. You need to draw a flowchart, decide which variables (RAM, working registers)..then write the code. The task is not difficult.

Klaus
 
You forgot to mention the targetted microcontroller. ASM language is different for each processor family.

Regarding the given code snippet, it misses a variable declaration, e.g. 8/16/32 bit data type, signed or unsigned.

For translating the code to assembler, you need to know the instruction set and syntax of the specific processor.

Here's example compiler output of XC8 for PIC18. I declared all variable as volatile to skip all optimisations, e.g. expecting variables being zeroed befor.

Code:
110:                   unsigned volatile char i,w,f,x,y,z,results;
111:               f = 0;
  FF7A    6A07     CLRF 0x7, ACCESS
112:               for (i=0; i<w; i++) {
  FF7C    6A06     CLRF 0x6, ACCESS
  FF7E    D011     BRA 0xffa2
  FFA0    2A06     INCF 0x6, F, ACCESS
  FFA2    5002     MOVF 0x2, W, ACCESS
  FFA4    5C06     SUBWF 0x6, W, ACCESS
  FFA6    E3EC     BNC 0xff80
113:               if (results >= 70)
  FF80    0E45     MOVLW 0x45
  FF82    6408     CPFSGT 0x8, ACCESS
  FF84    D002     BRA 0xff8a
114:               x = x + 1;
  FF86    2A03     INCF 0x3, F, ACCESS
  FF88    D00B     BRA 0xffa0
115:               else if(results >= 60)
  FF8A    0E3B     MOVLW 0x3b
  FF8C    6408     CPFSGT 0x8, ACCESS
  FF8E    D002     BRA 0xff94
116:               y = y + 1;
  FF90    2A04     INCF 0x4, F, ACCESS
  FF92    D006     BRA 0xffa0
117:               else if(results >= 50)
  FF94    0E31     MOVLW 0x31
  FF96    6408     CPFSGT 0x8, ACCESS
  FF98    D002     BRA 0xff9e
118:               z = z + 1;
  FF9A    2A05     INCF 0x5, F, ACCESS
  FF9C    D001     BRA 0xffa0
119:               else
120:               f = f + 1;
  FF9E    2A07     INCF 0x7, F, ACCESS
121:               }
--- Updated ---

The same code using labels and variable symbols, the usual relocatable assembler input.

Code:
;xc8_test.c: 110: unsigned volatile char i,w,f,x,y,z,results;
;xc8_test.c: 111: f = 0;
      clrf    main@f,c

;xc8_test.c: 112: for (i=0; i<w; i++) {
      clrf    main@i,c
      goto    l684
  l668:

;xc8_test.c: 113: if (results >= 70)
      movlw    69
      cpfsgt    main@results,c
      goto    l672

;xc8_test.c: 114: x = x + 1;
      incf    main@x,f,c
      goto    l682
  l672:

;xc8_test.c: 115: else if(results >= 60)
      movlw    59
      cpfsgt    main@results,c
      goto    l676

;xc8_test.c: 116: y = y + 1;
      incf    main@y,f,c
      goto    l682
  l676:

;xc8_test.c: 117: else if(results >= 50)
      movlw    49
      cpfsgt    main@results,c
      goto    l680

;xc8_test.c: 118: z = z + 1;
      incf    main@z,f,c
      goto    l682
  l680:

;xc8_test.c: 119: else
;xc8_test.c: 120: f = f + 1;
      incf    main@f,f,c
  l682:
      incf    main@i,f,c
  l684:
      movf    main@w,w,c
      subwf    main@i,w,c
      bnc    l668

;xc8_test.c: 121: }
 
Last edited:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top