Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

8086 Assembly Unsigned Addition

Status
Not open for further replies.

lord loh.

Member
After some work I finally managed to make a programme in assembly. Here is it....

Any comments? I am new to assebmly and would like to know if my code is okay and efficient or something needs to be done...

Code:
;Macros
%macro writeStringRD 1
				mov dx,%1
				mov ah,09h
				int 21h						;DOS Interrupt to erite a string ending in $
%endmacro

%macro readKBRD 1
				mov ah,0Ah
				mov dx,%1-1
				int 21h						;DOS Interrupt to take in vlues from the keyboard.
%endmacro
;CODE
section .text
org 100h
%include "definitions.asm"

				writeStringRD msg1
				readKBRD num1
	
				mov si,num1					;convert ASCII to hex
				call ascii2hex
				mov [n1],ax
	
				writeStringRD msg2
				readKBRD num2
	
				mov si,num2					;conver ASCII to hex
				call ascii2hex
				mov [n2],ax
				
				add ax,[n1]					;Add the two hex numbers

				mov di,num1-1				;Convert result to ASCII
				mov byte[di],'$'			;Terminating Character
				dec di						;Decrement pointer. The number is stored in the reverse direction in memory.
				call hex2ascii

				writeStringRD msg3
				inc di
				writeStringRD di

				int 20h						;EXIT

;Subroutines
ascii2hex:							;Assumes ASCII number pointer is present in SI
				xor ax,ax			;Clear AX
				mov ch,al			;Clear CH
				mov dx,ax			;Clear DX
				mov cl,[si]		;load ASCII number length in CL
				inc si				;pointer to ASCII number

.conversion:
				mov bx,10			;Multiply by 10;
				mul bx				;multiply unpacked BCD in AX by 10
				mov bl,[si]		;Get ASCII into BL
				cmp bl,39h			;Was that really an ASCII number?
				jg .invalid			;No?				
				sub bl,30h			;Convert to Unpacked BCD - as good as HEX


				add ax,bx			;ADD unpacked BCD to AL
				inc si				;Increment ASCII number pointer
				loop .conversion
				ret					;Return result in AX

.invalid:		sub sp,2			;restore stack pointer vlaue.
				writeStringRD msg4	;Display error message.
				int 20h				;EXIT

hex2ascii:							;Assumes the Destination pointer in DI
				mov bx,10			;divide by 10
.conversion:
				xor dx,dx			;clear DX
				div bx				;divide the HEX number by 10
				add dl,0x30			;Convert to ASCII
				mov [di],dl		;store the remainder as an unpacked BCD
				dec di				;decrement pointer. Number is stored in the reverse direction in memory.
				cmp ax,0			;Is the conversion over?
				jnz .conversion		;No?				
.finished:		ret

;Data
section .data
msg1: 	db "PROGRAMME FOR THE ADDITION OF TWO UNSIGNED NUMBERS",cr,lf,"by Bharath Bhushan Lohray"
		db cr,lf,lf,"Enter the first number :$"
msg2: 	db cr,lf,"Enter the second number :$"
msg3:	db cr,lf,"The sum of the two numbers is :$"
msg4:	db cr,lf,"Invalid Input$";

section .bss
result:	resw 3
num1: 	resw 4
num2:	resw 4
n1: 	resw 1
n2:		resw 1

definitions.asm includes the definition for CR and LF
cr EQU 10
lf EQU 13
newline EQU 0x0A0D

All works fine in this code...

The trouble is that newline does not always do crlf.... Just does LF
so output looks like
Code:
PROGRAMME FOR THE ADDITION OF TWO UNSIGNED NUMBERS
                                                    by Bharath Bhushan Lohray

What could be wrong?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top