0% found this document useful (0 votes)
46 views10 pages

Lab Session 5

Uploaded by

toobafar004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views10 pages

Lab Session 5

Uploaded by

toobafar004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

LAB SESSION 5

“Arithmetic Instruction in Assembly Language


Temperature Conversion from Celsius to Fahrenheit”

Objective
To learn how to perform arithmetic operations using assembly language and to convert a
temperature value from Celsius to Fahrenheit.
Background

Tools Required
EMU86 (or any compatible x86 emulator)
Basic knowledge of assembly language
Instructions to Run the Program
 Open EMU86.
 Create a new file and copy the above assembly code into it.
 Assemble the code using the EMU86 assembler.
 Link the program.
 Run the program and enter a temperature in Celsius when prompted.

Code Breakdown:
Data Segment Declaration (.DATA):
F DW ? Declares a word (16-bit) variable F that is uninitialized and will store the
result.
Code Segment Declaration (.CODE):

MOV AX,@DATA / MOV DS,AX: This is setting up the data segment so that the data
segment register (DS) can point to the .DATA segment for accessing variables like F.

Main Procedure (MAIN PROC):


MOV AX, 260: Load AX with the value 260.
MOV BX, 9: Load BX with the value 9.
MUL BX: Multiply AX by BX (260 * 9 = 2340). The result is stored in DX:AX (since it's a
32-bit result). In this case, DX would be 0 since the result fits in AX.
MOV BX,5: Load BX with the value 5.
DIV BX: Divide AX by BX (2340 / 5 = 468). The quotient is stored in AX, and the
remainder (modulo) is stored in DX. AX now holds 468.
MOV BX, 32: Load BX with the value 32.
ADD AX, BX: Add 32 to AX (468 + 32 = 500).
MOV BX, 1: Load BX with the value 1.
SUB AX, BX: Subtract 1 from AX (500 - 1 = 499).
MOV F, AX: Store the result (499) in the variable F.
Main Procedure End (MAIN ENDP):The procedure ends and control returns to the
operating system.

Adjusted Code Breakdown for Celsius to Fahrenheit Conversion:


MOV AX, 260:
This is the Celsius temperature you are converting (260°C in this case).

MOV BX, 9 / MUL BX:


Multiply Celsius (in AX) by 9.

MOV BX, 5 / DIV BX:


Divide the result by 5, giving you the first part of the conversion formula (Celsius×9/5)

MOV BX, 32 / ADD AX, BX:


Add 32 to the result to complete the conversion to Fahrenheit.

MOV BX, 1 / SUB AX, BX:


Subtracting 1 appears to be a manual adjustment, but in a typical conversion formula, you
wouldn't do this unless you're adjusting for rounding.

MOV F, AX:
Store the final Fahrenheit result in the variable F.
Expected Output

Conclusion
In this lab, you learned how to convert Celsius to Fahrenheit using basic arithmetic instructions
in assembly language.
Exercise

1. Change Celsius to Kelvin: Modify the program to convert a temperature from Celsius to
Kelvin instead of Fahrenheit. The formula for conversion is: K=C+273.15K = C +
273.15K=C+273.15 (You can round 273.15 to 273 for simplicity.)
2. Add a Welcome Message: Add a welcome message at the beginning of the program to
greet the user before asking for the Celsius temperature.
3. Display Celsius Input: After the user enters the Celsius temperature, display the input
back to the user before showing the converted value.

.
LAB SESSION 5

“Arithmetic Instruction in Assembly Language


Temperature Conversion from Celsius to Fahrenheit”

Exercise

1. Change Celsius to Kelvin: Modify the program to convert a temperature from Celsius to
Kelvin instead of Fahrenheit. The formula for conversion is: K=C+273.15K = C +
273.15K=C+273.15 (You can round 273.15 to 273 for simplicity.)
2. Add a Welcome Message: Add a welcome message at the beginning of the program to
greet the user before asking for the Celsius temperature.
3. Display Celsius Input: After the user enters the Celsius temperature, display the input
back to the user before showing the converted value.

Source Code:

org 100h

.model small

.stack 100h

.data

Greeting DB 13,10,'Hello! Welcome to Celcius to Kelvin Converter $'

EN DB 13,10,'Enter Temperature in Celsius $'

N DW ?

UN DB 13,10,'You Entered: $'

Result DB 13,10,'Kelvin Temperature is:$'

K DW 273

.CODE

MAIN PROC

mov dx , offset Greeting ;for printing string

mov AH,9

int 21h
mov dx , offset EN ;for printing string

mov AH,9

int 21h

mov N,0 ;N=0

mov BL,10 ;BL=10

INPUT: ;LABEL

mov AH,1 ;Input a character => stores in AL=2 e.g (input 2)

int 21h ;interupt

CMP AL,13 ;compare input with enter key

JE NEXT ;if jump equal to then it jumps on label next

SUB AL,30h ;sub AL=AL-48=AL-30h

mov AH,0 ;AH=0

mov CX,AX ;CX=AX => CX=2

mov AX,N ;AX=N => AX=0

mul BL ;multiply => AX = BL*AX = 10*0=0

ADD AX,CX ;Add => AX=AX +CX =0+2 =2

mov N,AX ;N=AX => N=2

JMP INPUT

NEXT:

MOV BX ,N

Add K,BX
LEA DX,UN

mov AH,9

int 21h

;output code celsius temperature

mov AX,N ;AX=N=>25

mov DX,0 ;DX=0

MOV BX,10 ;BX=10

mov CX,0 ;CX=0 => counter loop

L1:

div bx ;divide => AX=AX/BX =25/10

;incase of 8bit reg => AL=quotient , AH = remainder

;incase of 16bit reg => AX=quotient , DX = remainder

;AX =2, DX=5

push dx ;5 save in stack

mov dx,0 ;DX =0

mov ah,0 ;AX AH 00000000 AL=quotient

inc cx

cmp ax,0 ;(2==0)

jne L1 ;jump not equal => (AX !=0)

mov ah,2 ;output character

L2:

pop dx ;first time pop 2 and second time pop 5 =25


add dx,48

int 21h

loop L2

LEA DX,Result

mov AH,9

int 21h

;output code Kelvin temperature

mov AX,K ;AX=N=>25

mov DX,0 ;DX=0

MOV BX,10 ;BX=10

mov CX,0 ;CX=0 => counter loop

L3:

div bx ;divide => AX=AX/BX =25/10

;incase of 8bit reg => AL=quotient , AH = remainder

;incase of 16bit reg => AX=quotient , DX = remainder

;AX =2, DX=5

push dx ;5 save in stack

mov dx,0 ;DX =0

mov ah,0 ;AX AH 00000000 AL=quotient

inc cx

cmp ax,0 ;(2==0)

jne L3 ;jump not equal => (AX !=0)


mov ah,2 ;output character

L4:

pop dx ;first time pop 2 and second time pop 5 =25

add dx,48

int 21h

loop L4

ret

Output:
DEPARTMENT OF SOFTWARE ENGINEERING
BACHELORS IN SOFTWARE ENGINEERING
Course Code: SE-209
Course Title: Computer Architecture & Logic Design
Lab Rubrics

Criteria 0 - Unsatisfactory 1 - Satisfactory 2 – Excellent


No assembly code is Assembly code is written but Assembly code is
Assembly written, or the code does contains minor errors that written correctly,
Code Writing not compile. affect functionality. fully functional, and
well-structured.
Demonstrates little to no Demonstrates basic Demonstrates
Understanding understanding of key understanding of concepts comprehensive
of Concepts concepts. but lacks depth. understanding of
concepts, showing
depth and insight.
Unable to identify or Identifies some problems but Effectively identifies
Problem- solve problems related to requires assistance to solve and solves problems
Solving Skills the task. them. independently with
minimal guidance.
No documentation or Some documentation and Code is
Documentation comments present in the comments are present but welldocumented with
and Comments code. lack clarity. clear, concise
comments explaining
functionality.
Code is inefficient and Code is somewhat optimized Code is highly
Performance does not follow best but could benefit from optimized, utilizing
Optimization practices. improvements. best practices for
performance and
efficiency.

Total

Signature

You might also like