Click here to Skip to main content
15,748,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The challenge is:

Which button was pressed first? – Program in Assembly

Two players, A and B have a push button in front of them. After a signal from the referee, both players try to push their button first. The system should record the first button push and display the player's number.

Subsequent button pushes should be ignored.
The controller needs to be connected to two input buttons (A and B), as well as a two LEDs (X1, X0) for output.

Use the controller’s reset button for resetting the application.
If input A goes from 0 to 1 first, then X is set to 012 (and stays there).
If input B goes from 0 to 1 first, then X is set to 102 (and stays there)

Note: You will not need any gates, flip-flops or a display-decoder, but you will need resistors for inputs and outputs.

What I have tried:

I've tried this assembly code: but it keep showing three errors and I'm not sure how to solve these three errors:

; PORTB 6th and 7th pin are used to connect two LEDs.

; LED1 - PB6

; LED2 - PB7

; PORTD 2nd and 3rd pin are used to connect two input buttons. Which are used as interrupts and it generates interrupt during the rising edge of INT pin. That is from 0 to 1

; ButtonA - PD2 - INT0

; ButtonB - PD3 - INT1

          .org     0x00
           rjmp    reset
          .org     0x01
           rjmp    INT0_VECT
          .org     0x02
           rjmp    INT1_VECT
          .org     0x34
reset:     ldi     r16,(1<<ISC00) | (1<<ISC01) | (1<<ISC10) |(1<<ISC11) 
           out     EICRA, r16           ;the rising edge of INT1 generates an interrupt. 
           ldi     r16, (1<<INT0) | (1<<INT1) 
           out     EIMSK, r16           ;enables external interrupts
           ldi     r16, (1<<INTF0) | (1<<INTF1) 
           out     EIFR, r16            ;external interrupt flags are made high, when interrupt occurs it becomes low. 
           ser     r16
           out     DDRB, r16            ;PORTB as output
           clr     r18                  ;clears r18 register
           out     DDRD, r18            ;PORTD as input
           out     PORTB, r18           ;both LEDs are in off state
           sei                          ;enables global interrupt
here:      rjmp    here

           cpi     r18, 0x02
           breq    last
           ldi     r18, 0x01
           ldi     r16, (1<<PB6)        ;LED1 glows
           out     PORTB, r16
last       ldi     r16, (1<<INTF0) 
           out     EIFR, r16

           cpi     r18, 0x01
           breq    last1
           ldi     r18, 0x02
           ldi     r16, (1<<PB7)        ;LED2 glows
           out     PORTB
last1      ldi     r16, (1<<INTF1) 
           out     EIFR, r16


| gavrasm gerd's AVR assembler Version 5.4 (C)2022 by DG4FAC |
Compiling Source file: exp1.asm
Pass: 1

Error ==> last ldi r16, (1<<intf0)
[exp1.asm,44] 070:="" unknown="" instruction="" or="" macro!
error="="> out PORTB
[exp1.asm,56] 091: Missing or unknown parameter(s)!
Error ==> last1 ldi r16, (1<<intf1)
[exp1.asm,57] 070:="" unknown="" instruction="" or="" macro!
59="" lines="" done.

compilation="" aborted,="" 3="" errors!<="" blockquote="">

If there are helpful resources for learning Assembly language, especially in environments for Arduino Nano's it would be much appreciated! Thank You!
Updated 18-Sep-22 3:00am

1 solution

All labels in assembler must be terminated with a colon. You have two incorrect lines:
last       ldi     r16, (1<<INTF0) 


last1      ldi     r16, (1<<INTF1) 

Which should be:
last:       ldi     r16, (1<<INTF0) ; add the colon label terminator


last1:      ldi     r16, (1<<INTF1) ; add the colon label terminator
Share this answer
User-15002776 18-Sep-22 9:49am    
Thank you, I have checked and it still seems to have an error with the PORTB:

error="="> out PORTB
[exp1.asm,56] 091: Missing or unknown parameter(s)!

Any idea why that is? Thank you!
Richard MacCutchan 18-Sep-22 10:03am    
Yes, as stated in the error message, you have not told it what data to send via the out statement.
User-15002776 18-Sep-22 20:55pm    
I've fixed it by adding r16.

So it now is: out PORTB, r16

However this generate 24 lines of errors saying that: Source file not found: (no file)!

Richard MacCutchan 19-Sep-22 4:06am    
Sorry but we cannot guess what you are doing. Please use the Improve question link above, and add complete details of how you are running the build and the exact text of any error messages.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900