flat assembler
Message board for the users of flat assembler.
Index
> DOS > Print AX value |
Author |
|
avcaballero 13 Jan 2012, 07:25
Hello, I wrote this code some time ago. Nasm sintax. Maybe it could help you.
Regards
|
|||||||||||
13 Jan 2012, 07:25 |
|
freecrac 13 Jan 2012, 09:07
Masood.Sandking wrote: hi, i use this code to print AX value, but it can't be greater than 99..... This is because you only divide by 10, but the maximum value in AX can be up to 65536, so you have to divide the value in AX at first by 10.000 to become at last 5 decimal ASCIIs(maybe the first are 0) and not only 2 ASCIIs. Unsigned divide DX:AX by r/m word (AX=Quo, DX=Rem). Dirk |
|||
13 Jan 2012, 09:07 |
|
GhostXoPCorp 14 Jan 2012, 19:20
16 bits = 4 nibbles = FFFF = 0-65535 or a total of 65536 different numbers.
Division of the place value you your wanting to use, and putting the remainder into a buffer, and loop until ax = 0. Read up on how xor dx,dx is needed for word division and what cwd can do for you |
|||
14 Jan 2012, 19:20 |
|
shutdownall 15 Jan 2012, 01:10
You can use ax to easily carry values up to 9999.
Code: mov ax,9999h @@: and al,0fh add al,'0' <print> shr ax,4 jmp @b This will give wrong order of digits but print all. You can extend the code yourself, also calculating with bcd (binary coded decimals) is implemented in cpu and adjustment of digits after calculation. Here is a tutorial http://webster.cs.ucr.edu/AoA/Windows/HTML/AdvancedArithmetica6.html |
|||
15 Jan 2012, 01:10 |
|
LocoDelAssembly 15 Jan 2012, 02:42
Just in case this is not obvious for the OP the code above has several problems:
1. It will print trailing zeroes forever (unless <print> takes care of jumping out of the loop) 2. Ignoring problem 1., it will print only half of the digits of the numbers correctly, the other digits will always be printed as zero. 3. Assumes the numbers are in packed BCD form. 4. As already stated by the author, it prints the digits in the wrong order. |
|||
15 Jan 2012, 02:42 |
|
shutdownall 15 Jan 2012, 12:31
LocoDelAssembly wrote: Just in case this is not obvious for the OP the code above has several problems: This is a quick and dirty example that ax can hold more than only the value 99 and print out. Masood.Sandking can find more information in the bcd tutorial and should read it. And with respect, a little bit of programming he can do himself. Here corrected version of example regarding point 2. I think some small errors will teach the stuff better than just give a full working example. Assume that bp is not used in the <print> routine. This version will work even with value 0 for ax. But not killing leading 0's. Code: mov ax,9999h mov bp,4 @@: push ax and al,0fh add al,'0' <print> pop ax shr ax,4 dec bp jnz @b |
|||
15 Jan 2012, 12:31 |
|
Masood.Sandking 18 Jan 2012, 09:51
Thanks to all!
my problem solved. here is the code: Code: mov ax,[sum] mov bx,strsum inttostr: ;In bx the offset to the target string ;In ax the unsigned integer to convert to string ;It converts an unsigned integer to an ascii string push ax push bx push cx push dx push si xor cx, cx ;xor dx, dx THIS LINE IS BUGGY. mov si, 10 label2: ;I divide the number by 10 repeatedly ; and I put the remainders in the stack. cmp ax, 10 jb label1 ;THIS IS THE PLACE FOR xor dx, dx ;CLEAR THE PREVIOUS REMAINDER. xor dx, dx div si push dx inc cx jmp label2 label1: push ax inc cx label3: ;I retrive the numbers in the stack and ;I add to them the ascii value for '0' (character). pop dx add dx, '0' mov byte [ds:bx], dl inc bx dec cx jnz label3 ;The service 9h of the 21h interrupt prints a string ;terminated by the character '$' mov byte [ds:bx], '$' |
|||
18 Jan 2012, 09:51 |
|
freecrac 19 Jan 2012, 07:55
Masood.Sandking wrote:
Hint: For both instructions we donĀ“t need a segment override prefix for the DS-segment. .... Here is an example to print EAX. Code: ; print EAX DECOUT: mov di, ASCII mov cl, 0Ah ; 10 decimal ASCII mov ebx, 1000000000 P1: xor edx, edx div ebx add al, 30h mov esi, edx mov [di], al inc di mov eax, ebx mov ebx, 0Ah xor edx, edx div ebx mov ebx, eax mov eax, esi dec cl jnz P1 ;------- mov si, ASCII-1 mov dx, si inc dx ZERO: inc si cmp BYTE[si], "0" jz ZERO sub si, 0Ah cmp si, dx jz short SKIP add si, 0Ah mov dx, si SKIP: mov ah, 9 int 21h ret ASCII DB "0000000000", "$" Dirk Last edited by freecrac on 19 Jan 2012, 08:19; edited 1 time in total |
|||
19 Jan 2012, 07:55 |
|
Picnic 19 Jan 2012, 08:10
|
|||
19 Jan 2012, 08:10 |
|
GhostXoPCorp 21 Jan 2012, 08:38
Bravo Freecrac, you managed to do that in 2/3 the amount of code it took me.
_________________ Oh that divide overflow. Just jumps out of the bushes every time to scare the day lights out of me. |
|||
21 Jan 2012, 08:38 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.