How to Read Value in Assembly Arduino

AVR Assembly Programming Problems

Programming Problems

  • The following programming problems are designed for the Arduino Uno (ATmega328P) with the CSULB Shield. Programs may exist written in AVR Studio iv or Atmel Studio 6.
  • To display code, open in Chrome browser or download (Firefox).

1) In this programing problem yous volition write the assembly code needed to display a number between 0 and 9 every bit defined by the least significant 4 switches on your proto-shield (PINC). If the number is greater than nine plow ON the discrete LED wired to PORTB fleck 0, otherwise turn OFF the LED.

I have written much of the code, including calls to subroutines InitShield, WriteDisplay and BCD_to_7SEG. You should exist familiar with the outset two from your Lab piece of work. The BCD_to_7SEG subroutine takes as input a number between 0 and 9 in register r0. The subroutine then converts the decimal number into its respective vii segments and displays answer.

Every bit you write your plan recollect that:

  • The to the lowest degree meaning four switches are wired to PINC.
  • The error LED is wired to PORTB bit 0
  • BCD_to_7SEG'due south calling argument is in annals r0
  • Do not modify r16 when yous cheque to meet if it is less than 10

.INCLUDE  RST_VECT:    rjmp   reset .ORG 0x0100 .INCLUDE "spi_shield.inc" reset:    ldi    r16,low(RAMEND)    out    SPL,r16    ldi    r16,high(RAMEND)    out    SPH,r16 ; Initialize Proto-shield    telephone call   InitShield loop:    _____    r16, _____   // Read Switches from GPIO Registers    _____    r16, 0x___   // clear almost significant nibble    _____    r16, 0x___   // Is r16 less than ten10? (see notes)    _____   ___________   // unsigned conditional branch    _____   ______, ___   // fault - turn on the LED    rjmp    ___________   // see the flowchart no_error:    _____   ______, ___    // non an error - plow off the LED display:    _____   ____, _____    // transport statement to subroutine    telephone call   BCD_to_7SEG     // (encounter notes)    telephone call   WriteDisplay    rjmp    ____________

two) Write a subroutine named BlinkIt to complement a variable named blink and to and then send the least significant bit (b0)of blink, in SREG bit T, to a subroutine named TestIt. For the purpose of this test you practise not need to relieve registers on the stack for this question.

.DSEG Blink:  .BYTE   1 .CSEG BlinkIt:     ______   _________      // load variable to register xvi     ______   _________      // do something     ______   _________      // store register 16 back to variable     ______   _________      // store bit 0 to SREG T bit     ______   _________      // call TestIt using            relative            addressing mode     ret

Solution

Code

; ---------------------------------------- ; BlinkIt - TestIt ; Version 1.0 ; Date: 10/24/2014 ; Written By : Khoi Vu ; ----------------------------------------  .INCLUDE   .DSEG blink: .BYTE one  .CSEG .ORG 0x0000 RST_VECT: rjmp reset  .ORG 0x0100  .INCLUDE "spi_shield.inc"  reset: telephone call InitShield  clr spiLEDS // clear discrete LEDs test: rcall BlinkIt rjmp examination   BlinkIt: lds r16,blink com r16 // complement r16, since blink=0x00, output= 0xFF sts blink,r16 // store dorsum to blink bst r16,0 // store bit 0 of r16 to T rcall TestIt // call TestIt ret  TestIt: mov r16,spiLEDS // motion LEDs to register 16 sbr r16,0b10000000 // guessing LED is on brts washed // since chip 0 of r16 was i T=one so brts is gear up. it will branch to done cbr r16,0b10000000 // articulate r16 if non on washed: mov spiLEDS,r16 // move r16 back to LED rcall WriteDisplay // output to display ret

Simulation

Solution Package

3)  The following flowchart defines a subroutine named TestIt which is called past BlinkIt. TestIt therefore accepts the T chip as an argument. We are working with the Arduino Proto-shield (see Proto-shield Schematic). Translate the following flowchart into its equivalent AVR lawmaking. Your lawmaking must implement the flowchart on the right. For the purpose of this exam yous do not need to save whatever registers on the stack for this question.

.DEF  spiLEDS = r9

TestIt: ______   _________ ______   _________ ______   _________    ; approximate bit is set ;execute next line just if t = 0 ______   _________    ; guess is wrong
done: ______   _________ ______   _________ ret

Solution

Code

; ---------------------------------------- ; BlinkIt - TestIt ; Version 1.0 ; Engagement: 10/24/2014 ; Written By : Khoi Vu ; ----------------------------------------  .INCLUDE   .DSEG blink: .BYTE 1  .CSEG .ORG 0x0000 RST_VECT:     rjmp reset                 .ORG 0x0100                 .INCLUDE "spi_shield.inc"  reset: 	telephone call InitShield            	clr  spiLEDS	// clear discrete LEDs test: 	rcall BlinkIt 	rjmp  test   BlinkIt: 	lds r16,blink 	com r16			// complement r16, since glimmer=0x00, output= 0xFF 	sts blink,r16	// store back to blink 	bst r16,0		// store bit 0 of r16 to T 	rcall TestIt	// phone call TestIt     ret  TestIt: 	mov r16,spiLEDS		// move LEDs to register 16 	sbr r16,0b10000000	// guessing LED is on 	brts washed			// since bit 0 of r16 was 1 T=ane so brts is set. it will co-operative to done 	cbr	r16,0b10000000	// clear r16 if not on done: 	mov spiLEDS,r16		// move r16 back to LED 	rcall WriteDisplay	// output to display     ret

Simulation

Solution Parcel

4) Given variables A and B, each belongings an 8-bit signed 2'due south complement number. Write a program to observe the maximum value and put into variable C. Instance if A > B and then C = A.

Selection A: Bones implementation of if-then-else statement using load -> do something -> shop structure

Solution

Lawmaking

/* Given variables A and B, each property an eight-bit signed 2's complement number,   * write a program to find the maximum value and put into variable C. For   * example if A > B then C = A.  *  * Solution A: Basic implementation of if-and so-else statement  * using load -> do something -> store structure  */  .INCLUDE    .DSEG A:     .BYTE i B:     .BYTE 1 C:     .BYTE 1  .CSEG Max1: 	lds  r16,A    ; load 	lds  r17,B 	cp   r16,r17 	brlt elseMax1 ; if (A >= B)  notation: if A < B branch to else block 	mov  r18,r16  ; then C = A 	rjmp endMax1 elseMax1:      mov  r18,r17 	sts  C,r18    ; store endMax1:     rjmp Max1

Simulation

Solution Package

Option B: Bones implementation of if-and so-else statement. Structure modified to immediately

Solution

Code

/* Given variables A and B, each belongings an 8-chip signed 2'due south complement number,   * write a program to notice the maximum value and put into variable C. For   * instance if A > B so C = A.  *  * Solution B: Basic implementation of if-and so-else statement.  * Structure modified to immediately store upshot.  */ .INCLUDE    .DSEG A:     .BYTE ane B:     .BYTE i C:     .BYTE 1  .CSEG Max2: 	lds  r16,A    ; load 	lds  r17,B 	cp   r16,r17 	brlt elseMax2 ; if (A >= B) 	sts  C,r16    ; then C = A 	rjmp endMax2 elseMax2:     sts  C, r17 endMax2:

Simulation

Solution Package

Choice C: If-then-else argument restructured to if-then with approximate. Result immediately stored in SRAM.

Solution

Code

/* Given variables A and B, each belongings an 8-bit signed 2's complement number,   * write a programme to find the maximum value and put into variable C.   * For example if A > B then C = A.  *  * Solution C: if-then-else statement restructured to if-so with guess   * Issue immediately stored in SRAM.  */  .INCLUDE    .DSEG A:     .BYTE 1 B:     .BYTE ane C:     .BYTE ane  .CSEG  Max3: 	lds  r16, A    ; load 	lds  r17, B 	sts  C, r16    ; estimate A > B 	cp   r16, r17 	brge endMax3 	sts  C, r17 endMax3:     rjmp Max3

Simulation

Solution Packet

5)  Given variable A holds an 8-bit signed 2's complement number. Write a program to discover the absolute value A. Salvage upshot back into variable A.

A = |A|

Solution

Code

/* Program 2 Given variable A holds an 8-scrap signed 2's complement number,   *           write a program to find the absolute value A.   *           Save issue back into variable A.  *           A = |A|  */  .INCLUDE    .DSEG A:     .BYTE 1  .CSEG Absolute: 	lds  r16, A    ; load 	tst  r16       ; if (A < 0) 	brpl endAbs 	neg  r16       ; and so convert to a positive number endAbs:     sts  A, r16    ; store     rjmp Absolute

Simulation

Solution Package

6) Write a program to add together viii chip variables A and B together. Store the sum into 8 scrap variable C. For this programming problem you may assume that the sum is less than 255 if A and B are unsigned and between -128 and 127 if signed.

C = A + B

Solution

Code

/* Write a programme to add 8 fleck variables A and B together,  * and storing the sum into 8 bit variable C.  * For this programming problem yous may assume that the sum is less  * than 255 if A and B are unsigned and betwixt -128 and 127 if signed.  * C = A + B  */  .INCLUDE    .DSEG A:     .BYTE ane B:     .BYTE 1 C:     .BYTE i  .CSEG Adder88: 	lds  r0,A    ; load 	lds  r2,B 	add  r0,r2   ; add 	sts  C,r0    ; shop 	rjmp Adder88

Simulation

Solution Package

7) Write a programme to find the sum of unsigned 8 bit variables A and B. For this programming problem the sum may be greater than 255 if A and B. Store the sum into xvi chip variable C using little endian byte ordering.

C = A + B

Solution

Lawmaking

/* Write a programme to find the sum of 8 flake variables A and B.  * For this programming problem the sum may be greater than 255 if A and B  * are unsigned or less than -128 and greater than 127 if signed.  * Store the sum into 16 scrap variable C using little endian byte ordering.  * C = A + B  */  .INCLUDE    .DSEG A:     .BYTE i B:     .BYTE i C:     .BYTE two  .CSEG Adder816:     ; load     clr  r1       ; r1:r0 = 0:A 	lds  r0,A 	clr  r3       ; r3:r2 = 0:B 	lds  r2,B 	; add together 	add together  r0,r2    ; add together least significant bytes     adc  r1,r3    ; add with carry near pregnant bytes 	; store 	sts  C,r0     ; shop least pregnant byte first 	sts  C+one,r1 	rjmp Adder816

Simulation

Solution Parcel

8) Write a plan to notice the sum of signed 8 fleck variables A and B. For this programming problem the sum may be less than -128 and greater than 127. Store the sum into 16 scrap variable C using little endian byte ordering.

C = A + B

Solution

Code

/* Write a program to discover the sum of 8 fleck variables A and B.  * For this programming problem the sum may be less than -128 and greater than 127  * Shop the sum into sixteen bit variable C using fiddling endian byte ordering.  * C = A + B  */  .INCLUDE    .DSEG A:     .BYTE 1 B:     .BYTE i C:     .BYTE ii  .CSEG Adder816s:     ; load     clr  r17       ;  0:A 	lds  r16,A     ; Offset 8 bits are A 	clr  r19       ;  0:B 	lds  r18,B     ; First 8 bits are B 	; make variables 16-flake 	sbrc r16,7 	ser  r17 	sbrc r18,7 	ser  r19 	;add 	add  r16,r18 	adc  r17,r19 	;shop 	sts  C,r16     ; store the least pregnant byte 	sts  C+1,r17   ; store most pregnant bytes 	rjmp Adder816s

Simulation

Solution Package

nine) Multiply eight-scrap unsigned variables A and B placing the product into 16-bit variable C. Salvage the 16-flake product using piddling endian byte ordering.

C = A x B

Solution

Lawmaking

/* Multiply eight-bit unsigned variables A and B placing  * the product into 16-chip variable C.   * Save the 16-bit production using little endian byte ordering.  * C = A x B   */  .INCLUDE    .DSEG A:     .BYTE i B:     .BYTE one C:     .BYTE 2  .CSEG Mul8x8_16:     lds  r16,A    ; load 	lds  r17,B 	mul  r16,r17 	sts  C,r0     ; to the lowest degree significant byte (little terminate) 	sts  C+1,r1   ; near  significant byte (large end)  	rjmp Mul8x8_16

Simulation

Solution Package

10) Given 8-fleck variables A and B, each holding an viii-fleck unsigned Write a plan to find the boilerplate of A and B. Place the upshot into variable C.

Hint: Shifting (or rotating) a binary number to the left divides the number by ii.

Solution

Lawmaking

/* Given viii-bit unsigned variables A and B, each holding an 8-flake signed 2's complement number,   * write a program to observe the average of A and B and put the result into variable C.  * Hint: Shifting (or rotating) a binary number to the left is equivalent to dividing by 2.    */  .INCLUDE    .DSEG A:     .BYTE 1 B:     .BYTE 1 C:     .BYTE 1  .CSEG Avg: 	lds  r16, A    ; load 	lds  r17, B 	add  r16, r17  ; add     ror  r16       ; divide past 2 (include carry) 	sts  C, r16    ; store 	rjmp Avg

Simulation

Solution Package

11) Given 8-bit variables A and B, each belongings an eight-bit signed 2's complement number. Write a program to find the boilerplate of A and B. Place the result into variable C.

Hint: Shifting (or rotating) a binary number to the left divides the number by 2.

Solution

Code

/* Given 8-chip variables A and B, each belongings  * an eight-bit signed 2'southward complement number. Write  * a programme to find the average of A and B.  * Place the event into variable C.  */  .INCLUDE    .DSEG A:     .BYTE 1 B:     .BYTE i C:     .BYTE two  .CSEG  ; inputs: viii-bit variables A and B ; output: 16-flake register C Avg8s:     ; load registers A and B 	lds  r24,A 	lds  r26,B 	; find average C = A+B/2 	rcall Adder816s		; C=A+B 	;   divide by ii 	asr r25             ; least pregnant chip moved to carry fleck C 	ror r24		    	; carry moves into most significant chip of r24 	; store the 8 bit outcome 	sts  C,r24     	 	clr  r25     	sts C+1,r25	 	rjmp Avg8s  ; Add two 8-bit signed 2's complement numbers, ; where sum of A and B may be ix bits ; input: r24 and r26 are two 8-bit numbers ; output: register pair r25:r24 equals sum of r24 and r25 Adder816s: 	; make variables sixteen-bit 	clr  r25      	 	; guess r25 is positive 0x00:A 	sbrc r24,7          ; if number is positive guess is right so skip adjacent pedagogy 	ser  r25            ; gauge incorrect, number is negative 0xFF:A 	clr  r27 	sbrc r26,7 	ser  r27 	;add 	add  r24,r26 	adc  r25,r27 	;store 	sts  C,r24     		; store the to the lowest degree meaning byte 	sts  C+1,r25  		; shop virtually significant bytes                	ret

Simulation

Solution Package

12) Write a function named Div8_8 to divide an unsigned 8 bit number by an unsigned 8 bit number. You tin find this program in your textbook (Mazidi). Test your function past writing a program named Div8_8test to test the subroutine Div8_8 past dividing the viii-bit-number: 0xAA past the 8-flake-number 0x55.

Solution

Code

; Div8_8  ; Version 1.0 ; Appointment: Nov 11, 2014 ; Written Past : Yoseph Yegezu ; From text book 'The AVR Microcontroller and Embedded systems'Chapter 5 Page 167  .INCLUDE   			 .CSEG  .DEF Num=R20 .DEF Denominator=R21 .DEF Quotient=R22  .ORG 0x0000  	ldi Num, 0xAA		   	ldi Denominator, 0x55	   	//call the 8 bit division     rcall Div8     ret	 /************************************  * subroutine divides unside 8bit by 8bit  * Quotient = Numerator/Denominator *  *     r22  =     r20 / r21    * with residuum in r20            *  ************************************/  Div8:      clr  Caliber          // r22  // quotient is going to increment by 1 everytime L1 loops // loop  L1 stops when the numerator-denominator = less than the demoninator  L1: 	inc Quotient 	 	sub  Num,Denominator  // r20,r21 	brcc L1  //since the quotient is incremented by 1 when the loop began, after the loop caliber is dec 	dec  Quotient  //discover L1 is going to branch off when the numerator is no lnger divisiable by the denominator  //which means L1 is branching off when numerator-denominator results in a negative value. //therefore, the denominator is going to exist added to the numerator later the loop. 	add  Num,Denominator  // r20,r21 	 	ret

Simulation

Solution Package

13) Write a part named Div16_8 to divide an unsigned 16 scrap number by an unsigned 8 bit number. Test your function by writing a program named Div8_test to test the subroutine Div16_8 by dividing the sixteen-fleck-number: 0xAAAA by the 8-bit-number 0x55.

Choice A

Solution

Code

/*  * Write a subroutine named Div8 to divide a 16-bit number by an eight-scrap number.    * Next, write a program named Div8_test to test the subroutine Div8  * past dividing the 16-chip-number: 0xAAAA by the 8-fleck-number 0x55  *  * Q = North/D Split a xvi-bit-number NH:NL  by an 8-bit-number Q  *  * Source:   * one. Binary division in AVR Assembler  *    http://www.avr-asm-tutorial.internet/avr_en/calc/DIVISION.html  * 2. Integer division (unsigned) with remainder  *    http://en.wikipedia.org/wiki/Division_algorithm  */  .DEF NL = r0  ; LSB sixteen-chip-number to be divided .DEF NH = r1  ; MSB 16-fleck-number to be divided .DEF DIV = r3 ; 8-bit-number to carve up with .DEF QL = r4  ; LSB result .DEF QH = r5  ; MSB result  Div8_test: 	ldi r16,0xAA ; 0xAAAA to be divided 	mov NH,r16 	mov NL,r16 	ldi r16,0x55 ; 0x55 to be divided with 	mov DIV,r16 	rcall Div8	 	rjmp Div8_test    /* Div8   * Q = N/D Divide a 16-chip-number NH:NL  by an viii-flake-number Q   * input   *   N = Numerator (dividend)   *   D = Denominator (divisor)   * output   *   Q = Quotient    */ Div8:     push r0 	push r1 	push button r2  	clr r2 ; articulate interim register 	clr QH ; QH:QL = 0b0000 0000 0000 0001 	clr QL 	inc QL  div8a:         ; kickoff of the segmentation loop 	clc        ; clear carry-scrap 	rol NL     ; rotate the adjacent-upper flake of the numerator 	rol NH     ; to the acting register (multiply by 2) 	rol r2 	brcs div8b ; a one has rolled left, and so subtract 	cp r2,DIV  ; Sectionalisation result 1 or 0? 	brcs div8c ; bound over subtraction, if smaller div8b: 	sub r2,DIV ; decrease number to split up with 	sec        ; set conduct-bit, result is a 1 	rjmp div8d ; bound to shift of the result bit div8c: 	clc        ; clear carry-bit, resulting scrap is a 0 div8d: 	rol QL     ; rotate comport-flake into issue registers 	rol QH 	brcc div8a ; as long as zero rotate out of the result 	           ; registers QH:QL go on with the division loop 	pop r2 	pop r1 	pop r0 	ret        ; Terminate of the partitioning reached  program8:

Simulation

Solution Package

Pick B

This solution is an extension of Div8_8

Solution

Code

; Div16_8B  ; Version 1.0 ; Date: November 11, 2014 ; Written By : Yoseph Yegezu  .INCLUDE  		 .CSEG  .DEF Denominator=R19 .DEF NL=r24 .DEF NH=r25 .DEF QL=r21 .DEF QH=r22  .ORG 0x0000  	ldi r16,0xAA 	mov NH,r16 	mov NL,r16 	ldi Denominator, 0x55 	//Call the xvi chip by 8 flake division     rcall Div16_8 	ret	  /************************************  * subroutine divides unside 16bit by 8bit  * Quotient = Numerator/Denominator    *  * r22:r21  = r25:r24 / r19        *  ***************************************/ Div16_8:    	clr  r22 	clr  r21            // loop  L1 stops when the numerator - denominator = less than the demoninator  L1: //QL is going to increment past 1, everytime L1 loops 	inc QL   //r22 	 //When QL reaches 255 or 0XFF then goes back to 0, QH is going to increment past 1 	cpi r21,0 	brne No_Inc 	inc QH   //r22  	No_Inc: 	sub  r24,Denominator  // r19 	sbc  r25,r2  	brcc L1  //Since r21 is incremented by ane when the loop began, afterward the loop r21 is decremented 	december  QL  //r21  //Discover L1 is going to branch off when the numerator is no lnger divisiable past the denominator  //Which means L1 is branching off when r24-denominator results in a negative value. //Therefore, the denominator is going to be added to the r24 after the loop. 	 	add  r24,Denominator  //r24,r19 	adc  r25,r2 	 	 	ret

Simulation

Solution Package

14) Write a subroutine that catechumen a temperature reading in Fahrenheit (variable F)to Celsius (variable C).

Solution

Code

; ConvertCtoF  ; Version one.0 ; Date: November eleven, 2014 ; Written By : Yoseph Yegezu   .INCLUDE   .DSEG 		C: .BYTE 1 		F: .BYTE ane			 .CSEG  .DEF Denominator=R19 .DEF Caliber=R22  .ORG 0x0000  //Input a celsius value into r17 TestConvertCtoF: 	ldi r17,74 	sts C, R17 	//call subroutine ConvertCtoF 	rcall ConvertCtoF 	rjmp TestConvertCtoF  /****************************  * subroutine converts a temperature reading in Celsius (variable C) to Fahrenheit (variable F).  * F=(C × 9/5) + 32 ==(C × 18/10) + 32  * Range for C input is from (0 to 124), since F max is 255  ****************************/      ConvertCtoF: 	//Load the C value into r18 	lds r18, C 	//Input the constant value xviii in reg. 16 	ldi r16,xviii 	//This part calculates (C*18) 	mul r18, r16 	//Move the products into r25H and r24L 	movw r25:r24, r1:r0 	//Input the denominator into r19 	ldi Denominator, ten  //r19,ten 	//Telephone call the xvi scrap by 8 bit sectionalisation     rcall Div16_8 	// add 32 to the quotient (xviii*C)/10 + 32     ldi  r26,32 	add  Caliber,r26   //r22,r26 	adc  r23,r2      //Store the answer into F 	sts  F, r22 	ret	  /************************************  * Quotient = Numerator/Denominator *  * r23:r22  = r25:r24 / r19   * r24 = remainder                  *  ************************************/ Div16_8:      clr  r2 	clr  r23 	clr  Quotient          // r22  // quotient is going to increment by 1 every fourth dimension L1 loops // loop  L1 stops when the numerator-denominator is less than the demoninator(10) //(eighteen*C)/10 L1: 	inc Caliber 	adc r23,r2  	sub  r24,Denominator  // r24,r19 	sbc  r25,r2  	brcc L1 //since the quotient is incremented by 1 when the loop began, after the loop quotient is december 	dec  Quotient 	sbc  r23,r2 //notice L1 is going to co-operative off when the numerator is no lnger divisiable by the denominator.  //which means L1 is branching off when r24-denominator results in a negative value. //therefore, the denominator is going to be added to the r24 after the loop. 	add  r24,Denominator 	adc  r25,r2 	 	ret

Simulation

Solution Package

15) Write a subroutine that catechumen a temperature reading in Celsius (variable C) to Fahrenheit (variable F).

Solution

Lawmaking

; ConvertFtoC  ; Version 1.0 ; Date: November 11, 2014 ; Written By : Yoseph Yegezu   .INCLUDE   .DSEG 		C: .BYTE 1 		F: .BYTE 1			 .CSEG  .DEF Denominator=R19 .DEF Quotient=R22  .ORG 0x0000   // Input a Fahrenheit value into r17 TestConvertFtoC: 	ldi r17,124 	sts F, R17 // Call subroutine ConvertFtoC 	rcall ConvertFtoC 	rjmp TestConvertFtoC  /****************************  * Subroutine converts a temperature reading in Fahrenheit (variable F) to Celsius (variable C).  * (F  -  32)  10  5/9 = C  * Range of F (32 to 255) therefore Cmax is 123.viii  ****************************/      ConvertFtoC: 	//Load the F value into r18 	lds r18, F 	//Input the constant value eighteen in reg. sixteen 	ldi r16, 32 	//This part calculates (°F-32) 	sub r18, r16 	ldi r20, 5 	//This function calculates (°F-32)*five 	mul r18, r20 	//Move the products into r25H and r24L 	movw r25:r24, r1:r0 	//Input the denominator into r19 	ldi Denominator, 9 	//Call the 16 fleck by 8 bit segmentation     rcall Div16_8     sts  C, r22 	ret	  /************************************  * Quotient = Numerator/Denominator *  * r23:r22  = r25:r24 / r19         *  ************************************/ Div16_8:      clr  r2 	clr  r23 	clr  Caliber          // r22  // Quotient is going to increment past one everytime L1 loops // Loop  L1 stops when the numerator-denominator = less than 10(the demoninator) // This role calculates (°F-32)*5/9 L1: 	inc Quotient 	adc r23,r2  	sub  r24,Denominator  // r19 	sbc  r25,r2  	brcc L1 // Since the quotient is incremented by i when the loop began, after the loop caliber is december 	dec  Quotient 	sbc  r23,r2 // Notice L1 is going to branch off when the numerator is no lnger divisiable by the denominator.  // Which ways L1 is branching off when r24-denominator results in a negative value. // Therefore, the denominator is going to be added to the r24 after the loop. 	add  r24,Denominator 	adc  r25,r2  	ret

Simulation

Solution Package

sixteen) Given variables A, B, and C; each holding an 8-chip unsigned number. Write a program to find the average of A to C, placing the result into variable D.

D = A + B + C / 3

Allow for a 16-bit interim sum and consequence.

Solution

Code

/* Given variables A, B and C, each holding an eight-bit signed 2's complement number,   * write a plan to find the average of A to C, placing the result into variable D.  * D = A + B + C / 3  * Permit for a xvi-bit interim sum and event.  * Tip: Copy and paste Div8 subroutine into the AvgABC.asm program.  */ .INCLUDE    .DSEG A:     .BYTE 1 B:     .BYTE 1 C:     .BYTE 1 D:     .BYTE 2  .CSEG Setup: 	ldi r16, 0x34 ;  52 variable default values 	sts A, r16 	ldi r16, 0x78 ; 120 	sts B, r16 	ldi r16, 0xBC ; 188 	sts C, r16    ; sum = 0x168 (360), average = 0x78 (120)   AvgABC:                ; load 	clr  r1    ; r1:r0 = 0:C 	lds  r0,A 	clr  r3    ; r3:r2 = 0:B 	lds  r2,B 	clr  r5    ; r5:r4 = 0:A 	lds  r4,C      add r0,r2  ; A = A + B     adc r1,r3 	add r0,r4  ; A = A + C  	adc r1,r5                ; numerator r1:r0   = A + B + C 	ldi r16,3  ; divisor       /r2 =          /3 	mov r3,r16 	rcall Div8 ; quotient  r4:r3 = r1:r0 / r2   	sts D,r4 	rjmp AvgABC  /* Div8  * Q = Northward/D Separate a 16-fleck-number NH:NL  past an viii-bit-number Q  * input  *   N = Numerator (dividend)  *   D = Denominator (divisor)  * output  *   Q = Quotient   */  .DEF NL = r0  ; LSB 16-bit-number to exist divided .DEF NH = r1  ; MSB sixteen-scrap-number to be divided .DEF DIV = r3 ; 8-bit-number to split up with .DEF QL = r4  ; LSB result .DEF QH = r5  ; MSB result  Div8:     push r0 	push r1 	push button r2  	clr r2 ; clear interim register 	clr QH ; QH:QL = 0b0000 0000 0000 0001 	clr QL 	inc QL  div8a:         ; start of the partition loop 	clc        ; clear carry-scrap 	rol NL     ; rotate the next-upper chip of the numerator 	rol NH     ; to the acting register (multiply by 2) 	rol r2 	brcs div8b ; a i has rolled left, then subtract 	cp r2,DIV  ; Division issue 1 or 0? 	brcs div8c ; bound over subtraction, if smaller div8b: 	sub r2,DIV ; subtract number to divide with 	sec        ; prepare behave-bit, issue is a 1 	rjmp div8d ; jump to shift of the result fleck div8c: 	clc        ; clear behave-scrap, resulting bit is a 0 div8d: 	rol QL     ; rotate carry-bit into issue registers 	rol QH 	brcc div8a ; as long equally zero rotate out of the result 	           ; registers QH:QL become on with the partitioning loop 	pop r2 	pop r1 	pop r0 	ret        ; End of the sectionalization reached

Simulation

Solution Package


More than Bug to be added…


Practice Bug: Interrupts

  1. Initialize Interrupt 1 (INT1) pin to generate an Interrupt on a ascension edge. Set all unused bits to default values.
    _____ R16, 0x____
    _____ EICRA, R16
  2. Initialize Interrupt 0 (INT0) pin to generate an Interrupt on a falling edge. Exercise not change the other bits (ISC11, ISC10) in the External Interrupt Control Annals A (EICRA). You should assume the previous code (problem 1) has been written (i.e., bits 1 and 0 cleared)
    _____ R16, EICRA
    _____ R16, 0x____
    _____ EICRA, R16
  3. Configure Pin 15 PB1 (OC1A/PCINT1) to generate an Interrupt whenever the pin changes state. Do non alter whatsoever other bits. To make things more interesting, do not use the SBR instruction.
    _____ R16, PCMSK0 ; load
    _____ R17, PCICR
    _____ R18, SREG
    _____ R16, 0x ; do something
    _____ R17, 0x____
    _____ R18, 0x____
    _____ PCMSK0, R16 ; store
    _____ PCICR, R17
    _____ SREG, R18

hartlinebusbashem.blogspot.com

Source: https://www.arxterra.com/classes/intro-to-arduino-assembly/programming-problems/

0 Response to "How to Read Value in Assembly Arduino"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel