0% found this document useful (0 votes)
55 views17 pages

Coal Lab 03 Solutions

Coal Lab 3 Pdf

Uploaded by

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

Coal Lab 03 Solutions

Coal Lab 3 Pdf

Uploaded by

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

COMSATS University Islamabad

Wah Campus

Guidelines for Students Using emu8086.inc in Emu8086

The emu8086.inc file is an include file that provides a


collection of pre-written macros and routines that simplify tasks
like printing strings, numbers, and moving the cursor on the
screen. This is very useful for beginners, as it abstracts away
some of the complexity involved in writing assembly code from
scratch.

Here are the key points to understand and guidelines for using
emu8086.inc effectively:

1. Including the File in Your Program


To use the macros and functions provided in emu8086.inc, include
it at the beginning of your code using the following directive:
asm
Copy code
include 'emu8086.inc'


● This makes functions like print_string, print_num, putc,
etc., available in your code.

2. Using print_string to Display Strings

● The print_string macro allows you to print a string on the


screen easily.

Example of printing a string:


asm
Copy code
include 'emu8086.inc'

.model small

.stack 100h

.data

myString db 'Hello, World!$'

.code

start:

mov ax, @data

mov ds, ax

; Print the string

print_string myString

; Terminate program

mov ah, 0x4C

int 0x21


● Notice the $ symbol at the end of the string. This is
important because print_string uses the '$' terminator to
know where the string ends.
3. Printing Numbers Using print_num

● The print_num macro can display a 16-bit signed number.

Example of printing a number:


asm
Copy code
include 'emu8086.inc'

.model small

.stack 100h

.data

.code

start:

mov ax, @data

mov ds, ax

; Set number in AX register (e.g., 78)

mov ax, 78

print_num ax

; Terminate program

mov ah, 0x4C

int 0x21

● Important: Make sure the number fits within the signed 16-
bit range (i.e., -32768 to 32767). If you try to print
numbers beyond that range, it may display incorrectly.

4. Using putc to Display Characters

● putc is used to print a single character to the screen. You


place the ASCII value of the character in the al register.

Example of printing the character 'w':


asm
Copy code
include 'emu8086.inc'

.model small

.stack 100h

.code

start:

mov ax, @data

mov ds, ax

; Set the character 'w' in AL

mov al, 'w'

putc ; Prints 'w'

; Terminate program
mov ah, 0x4C

int 0x21


● This is useful when you need to print characters at specific
locations or control their placement more precisely.

5. Using gotoxy to Move Cursor

● The gotoxy macro moves the cursor to a specified row and


column on the screen.

Syntax:
asm
Copy code
gotoxy row, column

Example of using gotoxy with putc:


asm
Copy code
include 'emu8086.inc'

.model small

.stack 100h

.code

start:

mov ax, @data

mov ds, ax
; Move cursor to row 5, column 10 and print 'X'

gotoxy 5, 10

mov al, 'X'

putc

; Move cursor to row 8, column 15 and print 'Y'

gotoxy 8, 15

mov al, 'Y'

putc

; Terminate program

mov ah, 0x4C

int 0x21

6. Getting Input with scan_num

● The scan_num macro reads a signed integer from the user and
stores the result in the ax register.

Example of getting a number from the user and displaying it:


asm
Copy code
include 'emu8086.inc'
.model small

.stack 100h

.code

start:

mov ax, @data

mov ds, ax

; Get a number from the user

scan_num

; Display the number

print_num ax

; Terminate program

mov ah, 0x4C

int 0x21


● Note: You need to handle input carefully, especially when
dealing with signed numbers.

7. Handling Large Numbers with print_num

● If you encounter a situation where print_num displays a


negative number when trying to print a large positive value,
it’s due to overflow (as explained earlier). Ensure that the
number you're printing fits within the 16-bit signed range.

8. Combining print_string with Line Breaks

● To display multi-line text using print_string, you can


include newline characters (ASCII code 0x0D for carriage
return and 0x0A for line feed) in your strings.

Example:
asm
Copy code
include 'emu8086.inc'

.model small

.stack 100h

.data

message db 'First Line', 0Dh, 0Ah, 'Second Line', 0Dh, 0Ah,


'Third Line$'

.code

start:

mov ax, @data

mov ds, ax

; Print the multi-line message

print_string message
; Terminate program

mov ah, 0x4C

int 0x21

Summary:

● Include emu8086.inc at the beginning of your code to


simplify common tasks.
● Use print_string to print strings, ensuring the string ends
with a $.
● Use putc to print individual characters.
● Use gotoxy to position the cursor before printing with putc.
● Use print_num to display numbers, ensuring they fit within
16-bit signed range.
● Use scan_num to get input from the user.

By following these guidelines, students can effectively use


emu8086.inc to simplify their assembly programs in Emu8086.

How to fix overflow?

Explain 16-bit signed math

mov ax, @data

mov ds, ax

; Print the string

print_string myString
; Terminate program

mov ah, 0x4C

int 0x21


● Notice the $ symbol at the end of the string. This is important because
print_string uses the '$' terminator to know where the string ends.

3. Printing Numbers Using print_num

● The print_num macro can display a 16-bit signed number.

Example of printing a number:


asm
Copy code
include 'emu8086.inc'

.model small

.stack 100h

.data

.code

start:

mov ax, @data

mov ds, ax
; Set number in AX register (e.g., 78)

mov ax, 78

print_num ax

; Terminate program

mov ah, 0x4C

int 0x21


● Important: Make sure the number fits within the signed 16-bit range (i.e., -32768 to
32767). If you try to print numbers beyond that range, it may display incorrectly.

4. Using putc to Display Characters

● putc is used to print a single character to the screen. You place the ASCII value of the
character in the al register.

Example of printing the character 'w':


asm
Copy code
include 'emu8086.inc'

.model small

.stack 100h

.code

start:

mov ax, @data


mov ds, ax

; Set the character 'w' in AL

mov al, 'w'

putc ; Prints 'w'

; Terminate program

mov ah, 0x4C

int 0x21


● This is useful when you need to print characters at specific locations or control their
placement more precisely.

5. Using gotoxy to Move Cursor

● The gotoxy macro moves the cursor to a specified row and column on the screen.

Syntax:
asm
Copy code
gotoxy row, column

Example of using gotoxy with putc:


asm
Copy code
include 'emu8086.inc'

.model small
.stack 100h

.code

start:

mov ax, @data

mov ds, ax

; Move cursor to row 5, column 10 and print 'X'

gotoxy 5, 10

mov al, 'X'

putc

; Move cursor to row 8, column 15 and print 'Y'

gotoxy 8, 15

mov al, 'Y'

putc

; Terminate program

mov ah, 0x4C

int 0x21

6. Getting Input with scan_num


● The scan_num macro reads a signed integer from the user and stores the result in the
ax register.

Example of getting a number from the user and displaying it:


asm
Copy code
include 'emu8086.inc'

.model small

.stack 100h

.code

start:

mov ax, @data

mov ds, ax

; Get a number from the user

scan_num

; Display the number

print_num ax

; Terminate program

mov ah, 0x4C

int 0x21


● Note: You need to handle input carefully, especially when dealing with signed numbers.

7. Handling Large Numbers with print_num

● If you encounter a situation where print_num displays a negative number when


trying to print a large positive value, it’s due to overflow (as explained earlier). Ensure
that the number you're printing fits within the 16-bit signed range.

8. Combining print_string with Line Breaks

● To display multi-line text using print_string, you can include newline characters
(ASCII code 0x0D for carriage return and 0x0A for line feed) in your strings.

Example:
asm
Copy code
include 'emu8086.inc'

.model small

.stack 100h

.data

message db 'First Line', 0Dh, 0Ah, 'Second Line', 0Dh, 0Ah,


'Third Line$'

.code

start:

mov ax, @data

mov ds, ax
; Print the multi-line message

print_string message

; Terminate program

mov ah, 0x4C

int 0x21

Summary:

● Include emu8086.inc at the beginning of your code to simplify common tasks.


● Use print_string to print strings, ensuring the string ends with a $.
● Use putc to print individual characters.
● Use gotoxy to position the cursor before printing with putc.
● Use print_num to display numbers, ensuring they fit within 16-bit signed range.
● Use scan_num to get input from the user.

By following these guidelines, students can effectively use emu8086.inc to simplify their
assembly programs in Emu8086.

How to fix overflow?

Explain 16-bit signed math

ChatGPT can make mistakes. Check impo

LAB TASKS
1. Copy & paste the above programs to Emu8086 source code editor, and press
[Compile and Emulate] button. Run it!
2. Modify the above code to print your name on the screen. (Using print_string)
3. Modify the above codes to display a message showing more than one line on the
screen? (Using print_string)
4. Display the character ‘w’ on screen using putc?
5. Show 3 examples of use of gotoxy in combination with putc ?
6. Display the number 78 on screen using print_num?
7. Get a number from user using “scan num” and display it on screen using
“printnum” (without any messages for input and output by the help of nextline
ascii code)?

8. When Printnum tries to display“35000” the screen display is negative. Why?

< ------ >

You might also like