Write simple assembly programs for 16 bit arithmetic operations

 ; Addition

ORG 0H ; Set the origin address

mov r0,#34h //lower nibble of No.1 

mov r1,#12h //higher nibble of No.1 

mov r2,#0dch //lower nibble of No.2 

mov r3,#0feh //higher nibble of No.2 

clr c 

mov a,r0 

add a,r2 

mov 22h,a 

mov a,r1 

addc a,r3 

mov 21h,a 

mov 00h,c

end 


; Subtraction

ORG 0H ; Set the origin address

mov r0,#0dch //lower nibble of No.1 

mov r1,#0feh //higher nibble of No.1 

mov r2,#34h //lower nibble of No.2 

mov r3,#12h //higher nibble of No.2 

clr c // 

mov a,r0 

subb a,r2 

mov 22h,a 

mov a,r1 

subb a,r3 

mov 21h,a 

mov 00h,c 

end



; Multiplication

ORG 0H ; Set the origin address


MOV DPTR, #OPERAND1 ; Load the address of the first operand into DPTR

MOVX A, @DPTR ; Load low byte of first operand into accumulator

MOV R0, A ; Store low byte in register R0


INC DPTR ; Move to the high byte of the first operand

MOVX A, @DPTR ; Load high byte of first operand into accumulator

MOV R1, A ; Store high byte in register R1


MOV DPTR, #OPERAND2 ; Load the address of the second operand into DPTR

MOVX A, @DPTR ; Load low byte of second operand into accumulator


MOV B, R0 ; Move low byte of the first operand to register B

MUL AB ; Multiply low bytes, result in A

MOV R2, A ; Store low byte of result in register R2


MOV A, R1 ; Move high byte of the first operand to accumulator

MOV B, R0 ; Move low byte of the first operand to register B

MUL AB ; Multiply high and low bytes, result in A

ADD A, R2 ; Add to the previously calculated low byte of result

MOV R2, A ; Store high byte of result in register R2


; Result is now in registers R2:R1 (high byte : low byte)


END


OPERAND1: DW 1234 ; Replace with your 16-bit operand 1

OPERAND2: DW 5678 ; Replace with your 16-bit operand 2


; Division

ORG 0H ; Set the origin address


MOV DPTR, #DIVIDEND ; Load the address of the dividend into DPTR

MOVX A, @DPTR ; Load low byte of dividend into accumulator

MOV R1, A ; Store low byte in register R1


INC DPTR ; Move to the high byte of the dividend

MOVX A, @DPTR ; Load high byte of dividend into accumulator

MOV R2, A ; Store high byte in register R2


MOV DPTR, #DIVISOR ; Load the address of the divisor into DPTR

MOVX A, @DPTR ; Load low byte of divisor into accumulator

DIV AB ; Divide low bytes, quotient in A, remainder in B

MOV R0, A ; Store low byte quotient in register R0


INC DPTR ; Move to the high byte of the divisor

MOVX A, @DPTR ; Load high byte of divisor into accumulator

DIV AB ; Divide high bytes, quotient in A, remainder in B

MOV R1, A ; Store high byte quotient in register R1


; Quotient is in registers R1:R0 (high byte : low byte), remainder is in B


END


DIVIDEND: DW 5678 ; Replace with your 16-bit dividend

DIVISOR: DW 1234 ; Replace with your 16-bit divisor


Comments