Deptt of Computer Science Engineering & IT, Juit: 10B28CI481: Computer Organization Lab
Deptt of Computer Science Engineering & IT, Juit: 10B28CI481: Computer Organization Lab
Deptt of Computer Science Engineering & IT, Juit: 10B28CI481: Computer Organization Lab
JUIT
10B28CI481: Computer Organization Lab
Instructor: Jagpreet Sidhu
1
Practical #1 : Getting aquainted with the microprocessor architecture using debug.
Objective
The goal of this lab is to help you become acquainted with the Debug utility
If you don't understand all the concepts used in this lab, that's ok. You will learn more about
registers, data, offsets, byte swapping, and machine code in the lectures and you'll get lots of
practice with these concepts when doing assignments and other practicals.
Instructions
1. Open a DOS window on your PC. At the DOS prompt, type "debug" to start up the
debug utility.
2. The register command is used to initialize the registers used by your assembly
program. Registers are high-speed memory locations used to hold data (and in some
cases, commands). Use the following commands to set up your program:
a. Set up the data segment using "r DS" and entering a new value for DS, the
Data Segment register . Set it to equal 124Ch (the previous DS value shown in
the example is 12E6, you may see a different number).
r ds
DS 12E6
:124c
b. Set up the code segment using "r CS" and entering a new value for CS, the
Code Segment register, of 1774h.
r cs
CS 12E6
:1774
c. Set up the location within the code segment where your program will be using
"r IP" and entering a new value for IP, the Instruction Pointer register, of Ch.
r ip
IP 0100
:c
2
3. If you want to see the values for all the registers, use the "r" command without giving
a register name. Try it out! You should see that DS, CS, and IP have the new values
you just gave them.
4. Enter some data for your program. We want a sixteen bit value equal to 1234h. It's
going to be stored at the location offset from the start of the data segment by 4 bytes
(a byte is a 8 bit value). Addresses are specified in a Segment:Offset format where the
segment is the base of the address and the offset is the value that needs to be added to
it. Since DS points to the start of the data segment, the location four bytes from that
is specified by DS:4. This means the data is stored at the 20 bit address 124C4.
After entering 34, hit the space, then enter 12 and hit return.
e ds:4
124C:0004 00.34 00.12
You'll notice that to have 1234, you enter the 34, then the 12. This is called byte
swapping. If the 16 bit value is 1234 then it is entered 8 bits (one byte) at a time - first
the low byte, then the high byte.
d ds:4
Can you see where the data shows 34 12? The left-most column gives the segment
offset - 124C:0000 in the top row (recall, DS = 124C). If you count over five spaces
(start with zero), you should see the 34 12 for DS:0004 and DS:0005.
6. Now, we're going to enter a couple of machine code instructions. Machine code is
entered just like data except that instead of putting it in the data segment (by using
DS) you're going to put it in the code segment (by using CS). Use the following
commands:
e cs:c
1774:000C 00.bb 00.04 00.00 00.8b
1774:0010 00.07
7. You can use the "u" command to decode memory into assembly instructions. Use it to
look at the two instructions you just entered:
u cs:c
Instruction 1: ____________________
3
Instruction 2: ____________________
They should both be MOV instructions. The first one moves (copies) 4 into the BX
register. The second one moves the data "pointed to" by the BX register into the AX
register (the [BX] means that you are using data "pointed to" by BX).
8. Use the "r" command to look at the registers again. It will show the registers and the
assembly code for the command you are about to execute. You should see something
like this:
The BB0400 is the machine code. The "MOV BX, 0004" is the assembly code. In
debug, all numbers are displayed in hexadecimal.
Now, use the "t" command to trace through your program. After hitting "t", you will
see the registers displayed again, just like when you did the register command.
What are the IP, AX, and BX register values after the first "t" command?
IP = ___________
AX = __________
BX = ___________
What are the IP, AX, and BX register values after the second "t" command?
IP = ___________
AX = __________
BX = ___________
-- After each instruction, IP increments by the number of bytes of machine code that
were just executed. MOV BX, 0004 has machine code BB0400. That is three bytes
long (a byte is 8 bits). Prior to executing the instruction, IP was equal to C (12
decimal). After adding 3, IP should be equal to F (15 decimal).
-- When BX equaled 4 and you copied the value pointed to it into AX, the value in
AX was the 1234h you entered earlier. The bytes were swapped when it was copied
out of memory into the value we wanted!
4
Practical #2 : Print a Character
1. Given below is an assembly source code. Compile the program.
;=============================
;Print Character ----
;=============================
;
title PRTCHAR.ASM - Typical Minimal Assembly Program
;===============
.model small ;=
;===============
;======================
.data ;DATA + VARIABLES
;======================
print_msg1 db "Press a Key $"
CRLF db 13,10,"$"
print_msg2 db "Key pressed was: $"
key db ?
;======================
.stack 100h ;256-word stack;
;======================
;======================
.code ; START OF CODE
;======================
main: mov ax,@data ;Point ds at data segment
mov ds,ax ;Set ds:dx = far addr of msg
5
mov dx,offset CRLF ;DX = Addr of text to print
mov ah,9 ;AH = 9 "Print String Service"
int 21h ;CALL INT 21H
;=====================================
Your Tasks:
;=============================
;PrintString ----
;=============================
;title PRTSTR.ASM - Typical Minimal Assembly Program
;===============
.model small ;=
;===============
;======================
.data ;DATA + VARIABLES
;======================
6
hello_msg db "Hello world!$"
;======================
.stack 100h ;256-word stack;
;======================
;======================
.code ; START OF CODE
;======================
main: mov ax,@data ;Point ds at data segment
mov ds,ax ;Set ds:dx = far addr of msg
Your Tasks:
;=============================
;GETSTRING ----
;=============================
;
title GETSTR.ASM - Typical Minimal Assembly Program
;===============
.model small ;=
;===============
7
;======================
.data ;DATA + VARIABLES
;======================
;======================
.stack 100h ;256-word stack;
;======================
;======================
.code ; START OF CODE
;======================
main: mov ax,@data ;Point ds at data segment
mov ds,ax ;Set ds:dx = far addr of msg
inc bx ; bx = bx + 1
call getc ; read next character
mov byte ptr [bx], al ; In C: str[i] = al
jmp get_loop ; repeat loop test
get_fin: mov byte ptr [bx], '$' ; terminate string with 0
8
int 21h
;EXIT
mov ax,4c00h ;ah = code to return to DOS
int 21h ;al = 0, i.e., no error
;=====================================
getc: ; read character into al
push bx
push cx
push dx ;Save register values
mov ah, 01h ; 07h DOES NOT ECHO KEYBOARD
int 21h
pop dx
pop cx
pop bx ; Restore register values
ret
end main
Your Tasks:
1. Modify the program to get two strings from the user with two different prompt
messages
2. Make the program loop 5 times
9
Practical #5 : String instructions
Given below is an assembly source code. Compile the program.
.model small
.data
CR equ 13
LF equ 10
NewLine db CR,LF,"$"
.stack 100h
.code
10
rep movsb ; copy string1 into string2
jmp Next_Operation
Not_Equal:
mov ah,9 ; function 9 - display string
mov dx,OFFSET Message5 ; ds:dx points to message
int 21h ; call dos function
Next_Operation:
mov di,OFFSET SearchString ; make es:di point to string
mov cx,36 ; length of string
mov al,'H' ; character to search for
repne scasb ; find first match
jnz Not_Found
11
int 21h ; call dos function
jmp Lodsb_Example
Not_Found:
mov ah,9 ; function 9 - display string
mov dx,OFFSET Message7 ; ds:dx points to message
int 21h ; call dos function
Lodsb_Example:
mov ah,9 ; function 9 - display string
mov dx,OFFSET NewLine ; ds:dx points to message
int 21h ; call dos function
NextChar:
lodsb ; AL = next character in string
int 10h ; call BIOS service
loop NextChar
end start
Your Tasks:
1. Compile and Link this program.
2. Change the strings and check results
3 Try all string instructions learnt in the class.
4. Enter a character string from keyboard and search for a two character string to be
entered from keyboard.
12
Practical #6 : More Strings ,characters and arithmatics
Given below is an assembly source code. Compile the program.
.model small
.data
CR equ 13 ; carriage return
LF equ 10 ; line feed
firstPrompt DB CR,LF,"Enter the first number to add: $"
secondPrompt DB CR,LF,"Enter the second number: $"
answer DB CR,LF,"The answer is: $"
another DB CR,LF,"Do another? - $"
.code
.stack 100h
start:
mov ax,@data
mov ds,ax ; sets up the messages correctly
xor ax,ax
int 16h ; this gets a character from the keyboard
xor ax,ax
13
int 16h ; get char from keyboard
;Do another?
xor ah,ah
int 16h
mov bl,al ; save to bl
mov dl,al
mov ah,02h
int 21h ; display character
14
mov ax,4c00h
int 21h ; terminate program
end start
Your Tasks:
1. Compile and Link this Adder program.
2. Note the numbers must add up to less than Ten (Why?)
3. Modify the program so that "c" or "C" for continue is used rather than "s" or "S" for
stop.
.model small
.stack 100h
.data
new_file db "newfile"
attributes EQU 10h
.code
mov ax,04c00h
int 21h
end main
Your Task:
15
2. Modify the program to create a subdirectory “MYDIR” and create a file
“myfile.txt” into this subdirectory.
3. Write Your biodata in myfile.txt..
4. Read and display contents of myfile.txt
5.
Exercise a
; -------------------------
; FILE manager
; -------------------------
.model small
.stack 100h
.data
.code
;
________________________________________________________________________
__
;____________________PROCEDURES FROM
FILELIB.ASM___________________________
16
;
________________________________________________________________________
__
call NewLine
mov ax,04c00h
int 21h
end main
Exercise b
; -------------------------
; FILE library
; -------------------------
PUBLIC PrintMessage,GetString,GetChar,CreateFile
17
PUBLIC OpenFileRead,NewLine,WriteFile,CloseFile
Public Handle
.MODEL SMALL
.DATA
Handle dw ?
attributes EQU 10h
CR db 13
LF db 10
.code
;;SUBROUTINES USED:
;--------------------------------------------------------
PrintMessage:
push ax
push bx
push cx
push dx
mov dx,ax
mov ah,9
INT 21h
pop dx
pop cx
pop bx
pop ax
ret
;-----------------------------
GetString:
; read string terminated by keyboard CR into array
; whose address is in ax. Terminate it with 0.
push ax
push bx
push cx ; save registers
mov bx, ax
18
inc bx ; bx = bx + 1
call getchar ; read next character
mov byte ptr [bx], al ; In C: str[i] = al
jmp check_char ; repeat loop test
last_char:
mov byte ptr [bx], 0 ; terminate string with 0
pop cx
pop bx
pop ax ; restore registers
ret
;==========================================
getchar:
push bx
push cx
push dx
mov ah,1
int 21h
pop dx
pop cx
pop bx
ret
;==========================================
CreateFile:
push ax
push bx
push cx
push dx
mov dx,ax
mov cx, attributes
mov ah,03ch
int 21h
mov Handle,ax
pop dx
pop cx
pop bx
pop ax
ret
;==========================================
OpenFileread:
push ax
push bx
push cx
19
push dx
mov dx,ax
mov ah, 03dh ;Open File
mov al,2 ; 2=Read/Write
INT 21h
mov Handle,ax ; Save Handle to Variable
pop dx
pop cx
pop bx
pop ax
ret
;==========================================
NewLine:
push ax
push dx
mov dl,CR
mov ah,2
int 21h
mov dl,LF
mov ah,2
int 21h
pop dx
pop ax
ret
;==========================================
Writefile:
push ax
push bx
push cx
push dx
mov bx,Handle
mov dx,ax
mov ah,40h
mov cx,80h
int 21h
pop dx
pop cx
pop bx
pop ax
ret
;==========================================
CloseFile:
20
push ax
push bx
push cx
push dx
mov bx,ax
mov ah, 03eh ;Close File
INT 21h
pop dx
pop cx
pop bx
pop ax
ret
;==========================================
end
Your Tasks:
21
Practical #9 : Menu Structures
Given below are two assembly source codes Exercise a and Exercise b. Compile the
programs.
Exercise a
;
===============================================================
===
;=== MENU PROGRAM ===================
;
===============================================================
===
.model small ; MODEL TYPE
LineNo db ? ;variable
ColNo db ? ;variable
colour db ? ;variable
Test1 db " GOODBYE ",0
err1 DB " ERROR!! $"
store DB 4000 dup(?) ; save contents of screen
;____________________________________________________________________
22
;_____________________ SCREEN _______________________________________
Text4 db " "
Text db " x) exit "
Text2 db " 1) Option 1 "
Text3 db " 2) Option 2 "
Text6 db " 3) Option 3 "
Text5 db " 4) Option 4 "
Text7 db " 5) Option 5 "
Box1 db "ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ"
Box2 db "Û"
;
________________________________________________________________________
__
;
________________________________________________________________________
__
.code ; CODE SEGMENT
;
________________________________________________________________________
__
;____________________PROCEDURES FROM IOLIB
________________________________
23
;------<<<<<<<<<<<<<<<< GET KEYBOARD INPUT
>>>>>>>>>>>>>>>----------------
lop: call DISPLAY_MENU
call getc ; does not echo keyboard hit
;-------------------------------------------------------------------------
;
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<
cmp al,'X' ; is al= <<<<<<<<_X_>>>>>>>
jz exit ; if yes then exit <<<<<<<<<<<<<<<<<<<
cmp al,'x' ; is al= ?
jz exit ; if yes then same
;-------------------------------------------------------------------------
mov dh,13 ; row position of cursor
mov dl,26 ; col position
call cursor
;-------------------------------------------------------------------------
;
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<
; <<<<<<_1_ <<<
cmp al,'1' ; is al= 1 PRESSED <<<<<<<<<<<<<<<<<<<
jnz OVER
mov bp,OFFSET OnePressed
call DispMess
jmp lop
;-------------------------------------------------------------------------
;
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<
; <<<<<<_2_ <<<
OVER: cmp al,'2' ; is al= 2 PRESSED <<<<<<<<<<<<<<<<<<
jne OVER1 ;
mov bp,OFFSET TwoPressed
call DispMess
jmp lop
;-------------------------------------------------------------------------
;
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<
OVER1: cmp al,'3' ; is al= 3 PRESSED <<<<<<_3_ <<<
jne OVER2 ; <<<<<<<<<<<<<<<<<<
mov bp,OFFSET ThreePressed
call DispMess
jmp lop
24
;-------------------------------------------------------------------------
;
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<
; <<<<<<_4_<
OVER2: cmp al,'4' ; is al= 4 PRESSED <<<<<<<<<<<<<<<<<<
jne OVER3
mov bp,OFFSET FourPressed
call DispMess
jmp lop
;-------------------------------------------------------------------------
;
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<
; <<<<<<_5_ <<<
OVER3: cmp al,'5' ; <<<<<<<<<<<<<<<<<<<
jne OVER4
mov bp,OFFSET FivePressed
call DispMess
OVER4:JMP LOP ; REPEAT UNTIL SICK OF IT!
;-------------------------------------------------------------------------
;
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<
exit: mov bp, OFFSET XPRESSED
call DispMess
call Delay
call Delay
call Delay
;<<<<<<<<<<<<|===================|<<<<<<<<<<<<<<<<<<<<<<<<<<<
;============| CALLED PROCEDURES |===========================
;>>>>>>>>>>>>|===================|>>>>>>>>>>>>>>>>>>>>>>>>>>>
;============================================================
; MENU SCREEN PROCEDURE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
DISPLAY_MENU:
25
push bp
;----------------------------------------------------------
mov LineNo,15
mov bp,OFFSET Text ;displays intro message
call Disp
pop bx
ret
;----------------------------------------------------------
;==========================================================
; SCR BLANK PROCEDURE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;==========================================================
blank_scr:
push ax
push bx
push cx
push dx
CALL BLANK
pop dx
26
pop cx
pop bx
pop ax
ret
;----------------------------------------------------------
mov ColNo,25
MOV COLOUR,26
again: push cx
call disp
pop cx
inc LineNo
loop again
ret
;============================== paints BOX Top and bottom
TB: mov cx,2
mov LineNo,8
mov bp,OFFSET Box1 ; ES:BP points to message
mov ColNo,25
MOV COLOUR,15
B2: push cx
call disp
pop cx
add LineNo,8
loop B2
ret
;--------------------------------------------------------------
;============================== paints BOX sides
BS: mov cx,9
mov LineNo,8
mov bp,OFFSET Box2 ; ES:BP points to message
MOV COLOUR,15
B3: push cx
mov ColNo,24 ; do the left side
call dis2
27
mov ColNo,55 ; do the right side
call dis2
pop cx
inc LineNo ; move to the next line
loop B3
ret
;--------------------------------------------------------------
; display a string using int 10 function 13h
;--------------------------------------------------------------
Disp: push ax
push bx
push cx
push dx
mov ah,13h ; function 13 - write string
mov al,01h ; attrib in bl,move cursor
xor bh,bh ; video page 0
28
mov cx,1 ; length of string
29
DispMess: push ax
push bx
push cx
push dx
mov ah,13h ; function 13 - write string
mov al,01h ; attrib in bl,move cursor
xor bh,bh ; video page 0
;--------------------------------------------------------------
;--------------------------------------------------------------
;--------------------------------------------------------------
;--------------------------------------------------------------
end START_MAIN ; end prog
;--------------------------------------------------------------
;--------------------------------------------------------------
;--------------------------------------------------------------
Exercise b
; -------------------------
; IO library
; -------------------------
PUBLIC putchar,getchar,getc,blank,cursor,cursor2
PUBLIC putstring,getstring,newline,puts,delay,get_str
.MODEL SMALL
.DATA
sub_tot DW 0 ;intermediate value
flag DB 0 ;general purpose flag
30
LineNo db ?
; NEW <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;
.CODE
; -----------------------------------------------------------------------
delay: push ax
push bx
push cx
push dx ; creates 1 second delay
mov cx,800
delay0: push cx
mov cx, 0ffffh
delay1: loop delay1
pop cx
loop delay0
pop dx
pop cx
pop bx
pop ax
ret ;end proc
;--------------------------------------------------------------
putchar: push ax ;preserve registers used
push dx
mov dl, al ;DOS routine requires char in DL
mov ah, 02h ;set DOS function call number
int 21h ;call DOS function
pop dx
pop ax
ret
; -----------------------------------------------------------------------
getchar: mov ah, 0Ch ;set DOS function call numbers
mov al, 01h
int 21h ;call DOS function
ret ;return with char in AL
; -----------------------------------------------------------------------
; -----------------------------------------------------------------
putstring: push ax
push bx ; save registers use
31
je putstring2 ; if end of string exit
call putchar ; else print char
putstring2: pop bx
pop ax ; restore registers
ret
; -------------------------------------------------------------
getstring: push ax
push bx ; preserve registers
mov dx, ax
mov ah, 9
int 21h ; call ms-dos to output string
pop dx
pop cx
pop bx
pop ax ; Restore register values
ret
;--------------------------------------------------------------
newline: push ax
32
mov al, 10
call putchar
mov al, 13
call putchar
pop ax
ret
; -------------------------------------------------------------
getc: ; read character into al
push bx
push cx
push dx ;Save register values
mov ah, 01h ; 07h DOES NOT ECHO KEYBOARD
int 21h
pop dx
pop cx
pop bx ; Restore register values
ret
;--------------------------------------------------------------
blank: ;blanks screen
push ax
push bx
push cx
push dx
mov ah,2
mov bh,0
mov dx,0
int 10h
xor bh,bh
mov ah,8
int 10h
mov bl,bh
mov bh,ah
sub cx,cx
mov dx,184fh ;18h = 24 rows, 4f = 79 coulums
mov ax,0600h
int 10h
pop dx
pop cx
pop dx
pop ax
ret
33
; -------------------------------------------------------------
cursor:
push bx
push ax ;save these registers in case the calling
;program needs them
xor bh,bh
mov ah,02h
int 10h ;cursor move interrupt
pop ax
pop bx
ret
cursor2:
push bx
push ax ;save these registers in case the calling
;program needs them
xor bh,bh
mov ah,03h
int 10h ;cursor move interrupt
pop ax
pop bx
ret
; -------------------------------------------------------------
get_str: ; read string terminated by keyboard CR into array
; whose address is in ax. Terminate it with 0.
push bx
push bx
push cx ; save registers
mov bx, ax
inc bx ; bx = bx + 1
call getc ; read next character
mov byte ptr [bx], al ; In C: str[i] = al
jmp get_loop ; repeat loop test
34
get_fin: mov byte ptr [bx], 0 ; terminate string with 0
pop dx
pop cx
pop bx ; restore registers
ret
; -------------------------------------------------------------
END
Your Tasks:
1. Assemble menu.asm then
2. assemble iolib.asm using MASM
3. c:\hardware> LINK menu.obj iolib.obj to create menu.exe
4. MODIFY THE OPTIONS TO DO OTHER TASKS, NOT JUST PRINTING A
MESSAGE
5. Write how the menu was created.
;float.asm
; calculate equation with high precision without math coprocessor
; this program calculates linear equation: ax + b = 0
; the result is printed with floating point.
; for example: a = 7, b = 2
; x = -0.28571428....
name "float"
precision = 30 ; max digits after the dot.
dseg segment 'data'
cr equ 0Dh
lf equ 0Ah
new_line equ 0Dh,0Ah, '$'
mess0 db 'calculation of ax + b = 0', new_line
mess1 db 'enter a (-32768..32767)!', new_line
mess2 db lf, cr, 'enter b (-32768..32767)!', new_line
mess3 db cr, lf, cr, lf, 'data:', '$'
mess4 db cr, lf, ' a = ', '$'
mess5 db cr, lf, ' b = ', '$'
35
mess6 db cr, lf, 'result: ', cr, lf, ' x = ', '$'
mess7 db cr, lf, cr, lf, 'no solution!', new_line
mess8 db cr, lf, cr, lf, 'infinite number of solutions!', new_line
error db cr, lf, 'the number is out of range!', new_line
twice_nl db new_line, new_line
make_minus db ? ; used as a flag in procedures.
a dw ?
b dw ?
ten dw 10 ; used as multiplier.
four dw 4 ; used as divider.
dseg ends
; welcome message:
lea dx, mess0
call puts ; display the message.
36
lea dx, mess3
call puts
; check data:
cmp ds:a, 0
jne soluble ; jumps when a<>0.
cmp ds:b, 0
jne no_solution ; jumps when a=0 and b<>0.
jmp infinite ; jumps when a=0 and b=0.
soluble:
neg ds:b
mov ax,ds:b
xor dx, dx
; '-b' is in dx:ax.
; 'a' is in bx.
; 'x' is in ax.
; remainder is in dx.
37
push dx ; store the remainder.
pop dx
jmp end_prog
no_solution:
lea dx, mess7
call puts
jmp end_prog
infinite:
lea dx, mess8
call puts
end_prog:
lea dx, twice_nl
call puts
ret
start endp
;***************************************************************
38
; (-) / (-) = (+)
; (+) / (-) = (-)
cmp bx, 0
jns div_not_signed
neg dx ; make remainder positive.
div_not_signed:
push dx
; print dot after the number:
mov dl, '.'
call write_char
pop dx
;***************************************************************
39
; bx - divider.
; cx - maximum number of digits after the dot.
print_fraction proc near
push ax
push dx
next_fraction:
; check if all digits are already printed:
cmp cx, 0
jz end_rem
dec cx ; decrease digit counter.
mov ax, dx
xor dx, dx
cmp ax, 0
jns not_sig1
not dx
not_sig1:
jmp next_fraction
end_rem:
pop dx
pop ax
ret
print_fraction endp
;***************************************************************
40
; used with print_numx to print "0" and sign.
; this procedure also stores the original ax,
; that is modified by print_numx.
print_num proc near
push dx
push ax
cmp ax, 0
jnz not_zero
not_zero:
; the check sign of ax,
; make absolute if it's negative:
cmp ax, 0
jns positive
neg ax
;***************************************************************
41
; check if ax is zero, if zero go to end_show
cmp ax, 0
jz end_show
begin_print:
xor dx, dx
div bx ; ax = dx:ax / bx (dx=remainder).
skip:
; calculate bx=bx/10
push ax
xor dx, dx
mov ax, bx
div ds:ten ; ax = dx:ax / 10 (dx=remainder).
mov bx, ax
pop ax
jmp begin_print
end_show:
42
pop dx
pop cx
pop bx
ret
print_numx endp
;***************************************************************
;*******************************************************************
;***************************************************************
xor cx, cx
; reset flag:
mov ds:make_minus, 0
next_digit:
call read_char
43
je set_minus
; add al to cx:
xor ah, ah
add cx, ax
jc out_of_range ; jump if the number is too big.
jmp next_digit
set_minus:
mov ds:make_minus, 1
jmp next_digit
out_of_range:
lea dx, error
call puts
stop_input:
; check flag:
cmp ds:make_minus, 0
je not_minus
neg cx
not_minus:
pop ax
pop dx
ret
44
scan_num endp
;***************************************************************
;***************************************************************
cseg ends
end start
Your Tasks:
45