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 programming to assembly

Status
Not open for further replies.

nothing9099

Newbie level 4
Joined
Oct 25, 2017
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
78
I'm using a MSP430F5438A

I've been learning C for a little while and I've just started to learn assembler.

I have this question:Change the above code to turn on both LED 1 and LED 2 when both switches s1 and s2 are pressed (and turn both LEDs off otherwise)

Here is the code:
Code:
            ; CONFIGURE PORTS FOR INPUT AND OUTPUT
            bis.b   #0x01,&P1DIR            ; set P1.0 as output
            bic.b   #0x40,&P2DIR            ; set P2.6 as input
            bis.b   #0x40,&P2REN		  ; enable pullup for P2.6
            bis.b   #0x40,&P2OUT	        ; enable pullup for P2.6

sw_check:   bit.b   #0x40,&P2IN		  ; check if switch is pressed
	     jnz     led_off     	        ; if so jump to led_off

	          ; else
		    bis.b   #0x01,&P1OUT            ; turn on LED 1
		    jmp continue

led_off:    bic.b   #0x01,&P1OUT            ; turn off LED 1
continue:   jmp sw_check
            nop
I know how to do this question in C, but in assembler I'm not so sure. If you look at this line:

sw_check: bit.b #0x40,&P2IN ; check if switch is pressed

To check if both sw1 and sw2 are pressed in C, I would just put if ((P2IN & BIT6+BIT7) == BIT6+BIT7). What is the equivalent way of writing that in assembler?
 

I learnt turbo C and turbo assembler when young.

One interesting thing I learnt is that you can write a simple program in C and then print out the assembly code (there are several options; you can also use SDCC) and optimize manually and use in your program.

Oftentimes the code is not good looking or readable even; but it is usually efficient and you can just use the idea or adapt from the code.

But what I have learnt on the PC that it is not a good idea to check whether two keys are pressed "simultaneously"- you should press key1 first and while holding down key1 you need to press key2. Then release both (in any order) quickly.

You should similarly check both keypress and keyrelease and follow the order. But this is only a suggestion from some one who has not done programming for a long time.
 

To check if both sw1 and sw2 are pressed in C, I would just put if ((P2IN & BIT6+BIT7) == BIT6+BIT7). What is the equivalent way of writing that in assembler?
The question can't be answered without referring to the available bit processing instructions of a specific processor.

I don't fully understand how the quoted code snippet is related to your question, it contains other operations like GPIO initialization. It's showing bit operations with branches, a possible way to implement logic operations.

The operation can be coded as bitmask "and" with comparison. Don't know if it's the optimal implementation with MSP430 but it's at least possible. Something like

Code:
MOV.B P2IN, Rxx
AND.B #yy, Rxx
CMP.B #yy, Rxx
JNZ
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top