0

I want to display the value in the Al register which is in my case 0Ah and here's my code but nothig happens , I am not sure but I think that my problem is that I have a hexa number in the register but I can only print Char or strings so is there a way to convert the hexa number to a string for example

Here's the code I used :

; You may customize this and other start-up templates; 
; The location of this template is c:\emu8086\inc\0_com_template.txt

org 100h


.data segment

Array DB 0h,0h,0h,0h,0h 
x db 0h  
result db  ?

.code segment 

mov si,0 

loop1:
       ;these 5 lines allows me to take
       ;input and let it be shown on the screen

      mov ah,08h
      int 21h
      mov ah,02h
      mov dl,al
      int 21h

      ;those 3 lines allows me
      ;to start my code when
      ;enter button is pressed

      mov bl,0Dh
      cmp bl,dl
      JZ start

      ;those 4 lines allow me
      ;to enter 4 chars and
      ;fill an array with them
      ;to use them later

      mov array[si],dl
      inc si
      cmp si,5
      jne loop1 


start:

      ;putting each element in the
      ;array in a register to be 
      ;able to deal with it

      mov si,0
      mov al,array[si]
      mov bl,array[si+1]
      mov cl,array[si+2]
      mov dl,array[si+3] 

      ;subtracting 30h from each 
      ;number to turn it to its
      ;decimal form to deal with
      ;it as normal number

      sub al,30h
      sub bl,30h
      sub cl,30h
      sub dl,30h 

      ;adding all numbers in
      ;variable called result

      add al,cl
      add al,bl
      add al,dl

      ;printing

      mov ah,02h
      mov dl,al
      int 21h
      ret
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • Please post the code in image as text, thanks. – kgf3JfUtW Dec 21 '17 at 21:24
  • your code seems to have fundamental issues... is it COM or EXE model? If it is COM, you can't have `.data` at beginning, they will be executed as code (first instruction must be after `org 100h`, even if it just `jmp ...` over data to code). If it is EXE, you can't have `org 100h`, and you should set up `ds` before using it like `mov array[si],dl` ... I will try to ignore those and re-read what is your actual problem, but you should try few more tutorials and re-read everything, and check working correct examples few more times... – Ped7g Dec 22 '17 at 11:21
  • 1
    *";subtracting 30h from each number to turn it to its decimal form to deal with it as normal number"* - that's not accurate enough. You don't have numbers as inputs, but ASCII characters ("ASCII digits" when user was kind enough to give you correct input). By subtracting 30h from ASCII digit you will get binary (native way of CPU to encode integer values) value of the digit. I.e. `'5' - 30h = 5`. That value `5` is encoded in bits as `101`, not in decimal form. End of your code will try to display the binary result as ASCII character. If you will enter 9, 9, 9, 9, you will get `$` as result? – Ped7g Dec 22 '17 at 11:25
  • Because 4*9 = 36, and 36 is in ASCII encoding `'$'`. The `int 21h, ah=2` outputs ASCII character. If you want to see "36" on screen, you will have to split that binary value `36` into two characters `'3'` and `'6'`. There's about million of Q+A here on stack overflow dealing with output of numbers bigger than 10, and how to convert them into string/characters. See https://stackoverflow.com/tags/x86/info and search for part about "How do I handle multi-digit numbers?", for example this question: https://stackoverflow.com/a/40505938/4271923 – Ped7g Dec 22 '17 at 11:28
  • Possible duplicate of [Assembly 8086 | Sum of an array, printing multi-digit numbers](https://stackoverflow.com/questions/40503119/assembly-8086-sum-of-an-array-printing-multi-digit-numbers) – Ped7g Dec 22 '17 at 11:40

1 Answers1

0

First of all you forgot to include the last element of an array in your calculation.

Code to Include last element of array in your calculation:

Add this line where you are putting elements in registers:

mov bh, array[si+4]    ; store the last element in BH register

Add this line where you are converting content of register to decimal:

sub bh, 30h             ; convert it to decimal  

Add this line where you adding the contents of all the registers:

add al, bh              ; add contents of AL with contents of BL 

Code to print the sum in decimal:

  mov ah, 0     ; clear AH because DIV instruction takes AX for division
  mov cl, 10    ; store the divisor  in CL
  div cl        ; AX/CL remainder will be in AH & quotient in AL

  mov bx, ax    ; move AX value to BX because later instruction are going to overwrite value of AH 

  mov ah,02h     ; print quoteint
  mov dl,bl      
  add dl,30h     
  int 21h  

  mov ah,02h     ; print remainder 
  mov dl,bh
  add dl,30h
  int 21h

Since in your case the highest number after addition of all 5 element will be 2 digit number (9+9+9+9+9 = 45) we have to divide only once, print the quotient first and then the remainder. If the sum includes more than 2 digits (like 123) then for that you would have to repeat the same process continuously and store the remainder in the stack until you get zero as quotient. Then you can pop the digits from the stack & display them after doing necessary ASCII conversions.

Ahtisham
  • 9,170
  • 4
  • 43
  • 57