2

I need to convert 32-bit number to ASCII. I don't know how I can do it ....

This is the code what I wrote : The biggest number that could convert is 0001FFFF , if I set Dx to 000F and Ax to FFFF the assembler will give me a divide overflow error !!!

data segment  
    save db 10 dup(' ') , '$' ; the ASCII will save here
data ends

stack segment
    dw   128  dup(0)
stack ends

code segment
    assume cs:code , ds:data , ss:stack

    main proc far
    mov ax, data
    mov ds, ax
    mov es, ax



    mov di,offset save
    add di , 9

    mov ax,0ffffh
    mov dx,0001h


    mov cx,10  ; dx ax will divide by cx

    convert:
       div cx

       add dl,'0'
       mov [di],dl
       dec di

       mov dx,0



       cmp ax,cx
    jge convert

    add al,'0'
    mov [di] , al

    ;show the number
    mov ah,09h
    mov dx,offset save
    int 21h





    ; wait for any key....    
    mov ah, 1
    int 21h

    mov ax, 4c00h ; exit to operating system.
    int 21h    
    main endp
code ends    

end main

what should i do?

thanks in advance

Ali Bahrami
  • 5,935
  • 3
  • 34
  • 53
  • The main thing you need is a 32-bit divider, such as I described in a previous answer. That has the code in C, but it uses only operations that should be trivial to translate to assembly language. http://stackoverflow.com/questions/5386377/division-without-using/5387432#5387432 – Jerry Coffin Apr 15 '11 at 17:03
  • I hope I can do this :D, Jerry I think you what I need this converter for! last night you helped me with int21h and 4eh . Now I'm trying to display file sizes on the monitor but .... it's 32 bit and it's hard to divide :P – Ali Bahrami Apr 15 '11 at 17:15
  • Yup -- the "what" isn't too hard. I'm a bit less certain about the "why do this at all", part. 20 years ago, sure, but now? – Jerry Coffin Apr 15 '11 at 17:17
  • You know this is funny , Here in Iran we've got a course that called System Programming .... and you know what the project is? a program look like NC .... ( 20 years ago I was 1 year old ) – Ali Bahrami Apr 15 '11 at 17:20
  • @ALi: well, I guess that has some merit -- DOS did let you work at the system level as much as you wanted, and it's small enough to study the whole system in a semester or two, and reasonably plan on one person understanding virtually the whole system as well... – Jerry Coffin Apr 15 '11 at 17:23
  • You're right my friend. I wish I could use c/c++ in the project , but this is forbidden to use any other languages. – Ali Bahrami Apr 15 '11 at 17:29
  • @ALi: Yup -- you'll have to translate to assembly -- but it's mostly just using shifts, subtracts, and compares, so converting to assembly language is mostly just changing the syntax. – Jerry Coffin Apr 15 '11 at 17:35
  • @Jerry is shaving a few years off the delta when he says 20 years :-) – Pete Wilson Apr 15 '11 at 17:45

2 Answers2

2

Hehe that's an easy one :P

I believe you might be getting a 'Divide Error' but not specifically a 'Devide by ZERO error'

You say that you get an error when DX=0xF right?

Well 0x000FFFFF = 1048575 ... divide this by 10 you get >65535 ... The answer: you simply get a divide overflow. The result needs to fit within AX.

Rastikan
  • 517
  • 5
  • 18
  • Why would you get 0x000FFFFF if the biggest number you have to convert is 0x0001FFF ? Can you simply filter it out by masking only bit zero of the DX register ? – Rastikan Apr 15 '11 at 17:33
  • The file size stores in 32bit , I want to display file size on the monitor ... so I need to convert them into Base-10 ASCII and the biggest number should be FFFF'FFFF – Ali Bahrami Apr 15 '11 at 17:36
  • Yeah ... then, if you don't have access to extended E(AX,BX,CX etc) register it is more difficult. I'll check whether there is one such facility in on the BIOS or DOS interrupts. – Rastikan Apr 15 '11 at 17:50
  • HEY! This is such an odd combinaison 8086 (16bit registers) dealing with 32 bit values... this HAS BE be some homework from a cleaver teacher. It is actually not an obvious problem even for an experienced programmer. Your teacher wants you to figured it out :p – Rastikan Apr 15 '11 at 18:40
  • I might suggest one way of cheating yet still learning something: use some C compiler that is able to compile 8086 and write yourself the simplest 'atoi' example. Setup the project so it creates an .asm listing and see what it does. – Rastikan Apr 15 '11 at 18:41
  • lol , the the teacher didn't ask me to do that ... I myself want it to do ... anyways thanks for help ... anyways thanks for help – Ali Bahrami Apr 15 '11 at 18:42
  • BTW, I've checked DOS Interrupt 21h ... (because it does prints out file size that can be quite large) but couldn't find any facilities for easily converting to ASCII Decimal :( sorry ... I'd need to spend alot more time to write you a solution. It is a difficult problem. Why don't you have access to extended registers? – Rastikan Apr 15 '11 at 18:50
0

I'd say you'd put out a CR (015), an LF (012) and take the exit to BIOS.

Who'da thunk we'd ever see int 21 again?

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51
  • What!!?? Some anonymous BIOS/ASM-86 expert gave me a downvote? Is that any way to treat the guy who answers an obscure question? Meh! – Pete Wilson Apr 15 '11 at 17:04
  • @ALI - what do you WANT to do? Accept another character sequence? Or what? ALI, I KNOW IT WAS YOU!! No, just kidding. What is it you need to do? – Pete Wilson Apr 15 '11 at 17:04
  • @Pete , I just want to convert a 32bit dword into ASCII code to display it on the monitor. – Ali Bahrami Apr 15 '11 at 17:06
  • OK, so what's going on with the code you have? That is, what is it putting out? – Pete Wilson Apr 15 '11 at 17:07
  • @Pete: I downvoted you because this is not an answer, and I don't see the question as obscure at all: he stated what he's trying to do, gave the code, gave inputs that fail and explained how it fails. – BlueRaja - Danny Pflughoeft Apr 15 '11 at 17:09
  • @Pete: I wanted a code to convert a FFFF'FFFF number to ASCII but the biggest number that my code can convert is 0001'FFFF :( – Ali Bahrami Apr 15 '11 at 17:10
  • @BR & DP -- OP definitely hadn't explained the problem before his edit, so ... ah, never mind. – Pete Wilson Apr 15 '11 at 17:11
  • @ALI -- OK, do you want a base-10 number in the output string? Or a base-16 number? The essential way of handling both is the same, but one is easier than the other. – Pete Wilson Apr 15 '11 at 17:13
  • Forget it, just a slight bump in the road. – Pete Wilson Apr 15 '11 at 17:14
  • @ALI -- So it looks like, because you are working with 16-bit integers, you're going to have to do long division by 10. That is, a simple x/10 won't do it; you will also have to divide the upper 16 bits by 10 and handle them. Does that seem halfway right to you? I'm not sure I have the exact problem in hand, you see. – Pete Wilson Apr 15 '11 at 17:18
  • @Pete , I guess you're right. let's see , We've got a 32bit number that stored in DX-AX , How Can I convert it into ASCII (base-10)? – Ali Bahrami Apr 15 '11 at 17:24
  • @ALI -- Remember your long division from school? It's just like that (if I remember this process from many years ago :-). It would be something like: loop: savdx = dx; div dx by 10; take the remainder (ie, savdx-(dx*10)); form increment = dx-remainder; ax = ax + incrment; goto loop until *DX* is <= 0. Something along those lines, though I'm sure it's not perfectly correct. You are trying to mimic the long division you did in school. Does that sound reasonable to you? – Pete Wilson Apr 15 '11 at 17:35
  • @ALI -- I'll do this along with you, then we can BOTH get smarter :-) – Pete Wilson Apr 15 '11 at 17:39
  • @ALI -- oops, I'll take that back; Jerry is a lot smarter than us both -- it's a lot better to do it with subtracts, shifts, and compares -- which proves he's the MUCH better mentor. I'll just back out of this and complain about that DOWNVOTE, ali :-) – Pete Wilson Apr 15 '11 at 17:49
  • Of course he is. I've voted you up. I'm a newbie with assembly – Ali Bahrami Apr 15 '11 at 18:00
  • @ALi -- did you get what you needed? – Pete Wilson Apr 16 '11 at 00:02