The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: nayankumarp on February 28, 2008, 07:04:15 PM

Title: why result is not beeing displayed
Post by: nayankumarp on February 28, 2008, 07:04:15 PM
Hi All,
         I write this sample program for addition of two number that seems to be working fine but i am not able to see the output.

i am able to compile and run this following program but i am not able to see the result can anyone help me in solving this problem.

assume cs:code,ds:data

data   segment
opr1   dw 1234h
opr2   dw 0002h

result   dw 01 dup(?)

data   ends

code   segment

start:   mov ax,data
   mov ds,ax
   mov ax,opr1
   mov bx,opr2
   clc
   add ax,bx
   mov di,offset result
   mov [di],ax
   mov ah,4ch
   int 21h
code   ends
   end start

I would be very much thankfull for your help.

Regards
Title: Re: why result is not beeing displayed
Post by: nayankumarp on February 29, 2008, 03:25:28 AM
Plz help me  in understanding the problem because of which output is not beeing displayed.what's wrong i am doing or weather i am missing anything.

Unless you expert people will not help how can people like us who are beginner will learn.

Regards
Title: Re: why result is not beeing displayed
Post by: sinsi on February 29, 2008, 03:51:01 AM
1. convert the result into ascii
2. print the ascii
Title: Re: why result is not beeing displayed
Post by: MichaelW on February 29, 2008, 05:02:44 AM
And in case you have no idea how to do that, here is a very simple example, added to your code. I am assuming that you want to display the value in hex.

assume cs:code,ds:data

data   segment

  opr1      dw 1234h
  opr2      dw 0002h

  result    dw 0
  hexChars  db "0123456789ABCDEF"

data   ends

code   segment

start:
  mov ax,data
  mov ds,ax
  mov ax,opr1
  mov bx,opr2

  ;------------------------------------------------------
  ; There is no need to clear the carry flag here because
  ; ADD will set or clear it (and other flags) according
  ; to the results.
  ;------------------------------------------------------
  ;clc

  ;------------------------------------------------
  ; Perform the addition and save the value to data
  ; so the register can be used for other purposes.
  ;------------------------------------------------
  add ax,bx
  mov result, ax

  mov bx, result
  ;---------------------------------------------------------
  ; Each hex digit corresponds to 4 bits. To display the hex
  ; digits in the correct order the first digit displayed
  ; must be from the upper 4 bits of the value.  Shift the
  ; upper 4 bits of BX into the lower 4 bits. The shift will
  ; set all of the other bits to 0.
  ;---------------------------------------------------------
  mov cl, 12
  shr bx, cl
  ;----------------------------------------------------------
  ; Use the value now in BX as an index (offset) into the hex
  ; character table and copy the indexed character into DL.
  ; The indexing works because character "0" is at offset 0
  ; in the table, character "1" at offset 1, and so on.
  ;----------------------------------------------------------
  mov dl, hexChars[bx]
  ;------------------------------------------------------
  ; Use the DOS Display Character function to display the
  ; character now in DL.
  ;------------------------------------------------------
  mov ah, 2
  int 21h
  ;-------------------------------------------------------
  ; Repeat for the remaining hex digits. The smaller shift
  ; values will not set all of the other bits to 0, so an
  ; AND instruction is used to do this. For the lower 4
  ; bits no shift is necessary.
  ;-------------------------------------------------------
  mov bx, result
  mov cl, 8
  shr bx, cl
  and bx, 0fh
  mov dl, hexChars[bx]
  mov ah, 2
  int 21h

  mov bx, result
  mov cl, 4
  shr bx, cl
  and bx, 0fh
  mov dl, hexChars[bx]
  mov ah, 2
  int 21h

  mov bx, result
  and bx, 0fh
  mov dl, hexChars[bx]
  mov ah, 2
  int 21h

  ;------------------------
  ; Display a trailing "h".
  ;------------------------
  mov dl, "h"
  mov ah, 2
  int 21h

  mov ah,4ch
  int 21h

code   ends
end start

Title: Re: why result is not beeing displayed
Post by: nayankumarp on February 29, 2008, 04:40:31 PM
Hi Michael sinsi,
                        Thank you very much for your help .Its working now.

If i understand it correctly if i am declaring two variable as decimal like follow

opr1   dw 1234
opr2   dw 0003

then also i have to print the result in the same way as we are printing the result in hex we need to change the hexchar array to decimal array.

Regards
Title: Re: why result is not beeing displayed
Post by: MichaelW on February 29, 2008, 08:57:02 PM
To convert the value to decimal you must use a different method. The hex table method depends on each hex digit corresponding to 4 bits. For example, the value 1234h in binary is 0001001000110100b, or with the 4-bit groups separated for clarity:

0001 0010 0011 0100

But the value 1234d in binary is 0000010011010010b, and as you can see there is no correspondence between the decimal digits and the 4-bit groups.

The simplest conversion method that I know of extracts the decimal digits by repeatedly dividing the value by 10, continuing until the quotient is 0. After each division the remainder contains one of the decimal digits, for example:

1234 / 10 = 123 remainder 4
123 / 10 = 12 remainder 3
12 / 10 = 1 remainder 2
1 / 10 = 0 remainder 1

As you can see the digits are extracted in reverse order, but this is easily corrected by using the stack to reverse the order.
Title: Re: why result is not beeing displayed
Post by: neo85 on March 01, 2008, 03:26:56 AM
Here my function to convert a 16-bit unsigned interger to zero-terminated string:

;=======================================================================
;itoa: convert 16-bit unsigned integer to string
;input: ax (number)
; bx (radix)
;ouput: es:di (string)
;-----------------------------------------------------------------------
itoa proc
push cx
push dx
push di

xor cx, cx
@itoa_loop1:
xor dx, dx
div bx ;ax = quotient, dx = remainder
push dx
inc cx
test ax, ax
jnz @itoa_loop1

cld
@itoa_loop2:
pop ax
cmp al, 09h
jle @itoa_next
add al, 07h
@itoa_next:
add al, 30h
stosb
loop @itoa_loop2

xor al, al
stosb

pop di
pop dx
pop cx
ret
itoa endp


example 1:
mov ax, 100
mov bx, 10 ; decimal
call itoa

result: es:di -> "100"

example 2:
mov ax, 100
mov bx, 16 ; hexadecimal
call itoa

result: es:di -> "64"