Friday, September 1, 2023

Arithmetic operations in uvision keil using assembly language.

 To write assembly code for the LPC2148 microcontroller using Keil uVision, you'll need to use the ARM assembly language specific to this microcontroller and create a Keil project. Below is an example of assembly code for division, multiplication, subtraction, and addition using ARM assembly language in Keil uVision. This code assumes you have created a Keil uVision project for the LPC2148 and that you are familiar with the project setup.

Code:- 


AREA Arithmetic, CODE, READONLY
; Import the LPC2148 startup file
EXTERN __main
; Define memory addresses for input and output variables
; Modify these addresses as needed for your specific application
DIVIDEND EQU 0x40000000 ; Address of dividend
DIVISOR EQU 0x40000004 ; Address of divisor
QUOTIENT EQU 0x40000008 ; Address of quotient
REMAINDER EQU 0x4000000C ; Address of remainder
OPERAND1 EQU 0x40000010 ; Address of operand 1
OPERAND2 EQU 0x40000014 ; Address of operand 2
DIFFERENCE EQU 0x40000018 ; Address of difference
SUM EQU 0x4000001C ; Address of sum
ENTRY
; Division:
LDR r0, =DIVIDEND ; Load address of dividend into r0
LDR r0, [r0] ; Load the dividend value into r0
LDR r1, =DIVISOR ; Load address of divisor into r1
LDR r1, [r1] ; Load the divisor value into r1
MOV r2, #0 ; Clear quotient
MOV r3, #0 ; Clear remainder
CMP r1, #0 ; Check if divisor is zero
BEQ DIVISION_END
DIV_LOOP
    CMP r0, r1 ; Compare dividend with divisor
    BLT DIVISION_END ; If dividend < divisor, exit loop
    SUB r0, r0, r1 ; Subtract divisor from dividend
    ADD r2, r2, #1 ; Increment quotient
    B DIV_LOOP
DIVISION_END
STR r2, [QUOTIENT] ; Store quotient
STR r0, [REMAINDER] ; Store remainder
; Multiplication:
LDR r0, =OPERAND1 ; Load address of operand 1 into r0
LDR r0, [r0] ; Load operand 1 value into r0
LDR r1, =OPERAND2 ; Load address of operand 2 into r1
LDR r1, [r1] ; Load operand 2 value into r1
MUL r0, r0, r1 ; Multiply r0 by r1
STR r0, [SUM] ; Store the result in SUM
; Subtraction:
LDR r0, =OPERAND1 ; Load address of operand 1 into r0
LDR r0, [r0] ; Load operand 1 value into r0
LDR r1, =OPERAND2 ; Load address of operand 2 into r1
LDR r1, [r1] ; Load operand 2 value into r1
SUB r0, r0, r1 ; Subtract r1 from r0
STR r0, [DIFFERENCE] ; Store the result in DIFFERENCE
; Addition:
LDR r0, =OPERAND1 ; Load address of operand 1 into r0
LDR r0, [r0] ; Load operand 1 value into r0
LDR r1, =OPERAND2 ; Load address of operand 2 into r1
LDR r1, [r1] ; Load operand 2 value into r1
ADD r0, r0, r1 ; Add r1 to r0
STR r0, [SUM] ; Store the result in SUM
; Infinite loop to keep the program running
LOOP
    B LOOP
END


Please note that you need to modify the memory addresses (DIVIDEND, DIVISOR, QUOTIENT, etc.) to match your specific memory map and setup in your Keil uVision project. This code is intended to be used as a starting point, and you should adapt it to your specific requirements and hardware setup. Additionally, make sure you have the appropriate linker script and memory map configured in your Keil project.

No comments:

Post a Comment