I am trying to print the numbers simply in the sequence i.e

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

using Loop, First i converted each number into Hexa printed it reset it to the decimal increment by 1 and then print the next until the number is equal to 9, When the number is equal to 9 i used DAA to simply the number and after rotating and shifting the number i eventually stored the result in the string.

The output is just fine till the 16, but after 16 the sequence repeats itself,

**Desired output:**

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

**Current Output** 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,11,12,13,14,15

Why does it happens so ???

Here is my Code,

```
MOV CX,20 ;Number of Iterations
MOV DX,1
L1:
PUSH DX
ADD DX,30H
MOV AH,02H ;PRINT Content of DX
INT 21H
POP DX
ADD DX,1
CMP DX,09d ;If number is Greater than 9 jump to L2
JA L2
LOOP L1
L2:
PUSH DX
MOV AX,DX
DAA ;Convert to the Decimal
XOR AH,AH ;SET AH to 0000
ROR AX,1
ROR AX,1
ROR AX,1
ROR AX,1
SHR AH,1
SHR AH,1
SHR AH,1
SHR AH,1
ADC AX,3030h
MOV BX,OFFSET Result
MOV byte ptr[BX],5 ; Length of the String
MOV byte ptr[BX+4],'$' ;5th position of string , $=Terminator
MOV byte ptr[BX+3],AH ;2nd Number onto 4th position
MOV byte ptr[BX+2],AL ;3rd number onto 3rd Position
MOV DX,BX
ADD DX,02 ;1st 2 positions of String are type of string and
length respectively
MOV AH,09H ;to print the string
INT 21H
POP DX
ADD DX,1
LOOP L2
MOV AH,4CH ;Return control to the DOS
INT 21H
```

P.S: I took help from this chart in understanding the numbers.

http://www.cheat-sheets.org/saved-copy/ascii.png

Just giving it a try, though I'm not sure, and I can't quickly test this.

But instead of using two loops I'd recommend using one for the whole bunch of numbers.

Furthermore I have the feeling that the problem has to do with the `DAA`

instruction, which I'm not used to, since it is not supported in 64 bit mode.

Anyway, here's what I'd do:

```
mov cx,20
mov al,1
mov bl,10 ; divisor
mov bp,offset Result ; no need to load this in the loop!!!
L1: mov dx,ax ; save to register, not to stack
cmp ax,09d
ja L2 ; number has two digits
add al,30h ; ASCII addend
; insert your output code here
jmp L3 ; jump over the two digit code
L2: xor ah,ah
div bl ; divides AX by ten (no rotate or shift needed)
; quotient in AL, remainder in AH (correct order for little endian)
add ax,3030h
; insert your output code here (note that the buffer/string address is loaded to BP)
L3: mov ax,dx
inc ax
loop L1
; done
```

If you wouldn't mind if one-digit numbers had a leading zero, it'd be even easier.

The `div`

instruction is probably more expensive than `daa`

plus `ror`

plus `shr`

, but your quad-rotate/shift will be even worse :-/

(As I said, I could not try it... leaving this open to you... if it doesn't work, just ask back.)

—

[update:

Another approach, especially to spare the `div`

in this trivial case of digit separation, would be to add 6 to numbers greater nine (i. e. 10d = 0ah --(+6)--> 16d = 10h; this is what `daa`

also does), then you can get along with the rotate/shift combination you used before.

Even better were to add 246, then to `AX`

, after which you can simply use `ror ax,8`

(or `rol`

— doesn't matter in this case), i. e. 10d = 0ah --(+246)--> 256d = 100h, as well 15d = 0fh --(+246)--> 261 = 105h. Rotate it to be 0001h or 0501h respectively, add 3030h, and you're done.

/update]

[update level="2"

What the fun... I actually intended to write it in the first level update, but forgot it somehow: instead of `rol`

ling by 8, or — if your TASM really doesn't support `rol`

ling by immediate — eight times rolling by one, you can of course also make use of the `xchg`

instruction, which swaps values between registers, in this case

```
xchg al,ah
```

would do the job of swapping the contents of those two registers.

There's also a `bswap`

instruction for reversing the byte order within a register, but it's obviously only available for registers of 32+ bits width.

/update]

8086 code only allowed an immediate of 1 (or cl) for a count on shifts and rotates. To enable 286 code, tell Tasm ".286" at the top of your file. That's a guess.

The way I remember I used to print a two-digit number in al:

` ````
aam
add ax, 3030h
xchg al, ah
int 29h
mov al, ah
int 29h
```

```
.model small
.stack 100
.code
mov ax, 0ffffh ; hex number to find it's bcd
mov bx, 0000
mov dh, 0
l9 : cmp ax, 10000 ; if ax>10000
jb l2
sub ax, 10000 ; subtract 10000
inc dh ; add 1 to dh
jmp l9
l2 : cmp ax, 1000 ; if ax>1000
jb l4
sub ax, 1000
add bx, 1000h ; add 1000h to result
jmp l2
l4 : cmp ax, 100 ; if ax>100
jb l6
sub ax, 100
add bx, 100h ; add 100h to result
jmp l4
l6 : cmp ax, 10 ; if ax>10
jb l8
sub ax, 10
add bx, 10h ; add 10h to result
jmp l6
l8 : add bx, ax ; add remainder
; to result
mov ah, 02
mov cx, 0204h ; Count to display
; 2 digits
go: rol dh, cl
mov dl, dh
and dl, 0fh
add dl, 30h ; display 2 msb digits
int 21h
dec ch
jnz go
mov ch, 04h ; Count of digits to be
; displayed
mov cl, 04h ; Count to roll by 4 bits
l12: rol bx, cl ; roll bl so that msb
; comes to lsb
mov dl, bl ; load dl with data to be
; displayed
and dl, 0fH ; get only lsb
cmp dl, 09 ; check if digit is 0-9 or letter A-F
jbe l14
add dl, 07 ; if letter add 37H else only add 30H
l14: add dl, 30H
mov ah, 02 ; Function 2 under INT 21H (Display character)
int 21H
dec ch ; Decrement Count
jnz l12
mov ah, 4cH ; Terminate Program
int 21H
end
```

Similar Questions

How would I go about converting: <a class=btn btn-block btn-social btn-facebook> <i class=fa fa-facebook></i> Sign in with Facebook to embedded ruby? Right now I have the follo

I have a string (char) and I want to extract numbers out of it. So I have string: 1 2 3 4 /0 And now I want some variables, so I can use them as integer: a=1, a=2, a=3, a=4 How can I do that?

ive got an error to fix. its: [FreeTDS][SQL Server]Error converting characters into server's character set. Some character(s) could not be converted when i use turkish characters to insert i get thi

I have a column with 24 values from 00, 01, 02, .... , 23. What's the best way in MATLAB to convert to 00:00, 01:00, 02:00, 03:00 ... 23:00.

I am asked to convert a program to use generic types in my array. I have no idea where to start. I tried to convert the array to generic but I keep getting problems. I tried to just convert the array

Converting numbers from double-precision floating-point format to single-precision floating-point format results in loss of precision. What's the algorithm used to achieve this conversion? Are numbers

I have been figuring out how to scramble numbers from array after user enters 10 different numbers by using rand(). It crushes when it arrives to adjust() function so feel free to point out my stupid

I am trying to convert the plain text Arabic Numerals into Eastern Arabic digits. So basically taking 1 2 3... and converting them into ١ ٢ ٣.... The function converts all numbers, including any nu

i have two variables like: a=200 b=205 and want to find out all numbers between these two numbers (including. these specified numbers).

In excel I have a column of words which I need to convert to integers. For example, I have a column of industries: Capital Goods, List item, Consumer Services, Technology, etc. I want to replace e

I am trying to convert the alphabet to a string of number or integers. I can do it this way but I want to know if there is a better way of doing it? I have to convert 4 letters to the corrosponding nu

I've seen lots of info about converting a whole project to arc and then using flags to tell Xcode which ones not to convert. What about converting only individual files to arc? I tried it, and it runs

I am converting my app routines from ASIHTTP to AFNetworking due to the unfortunate discontinuation of work on that project ... and what I found out later to be the much better and smaller codebase of

This question already has an answer here: How to convert number to words in java 21 answers My program is to convert numbers between 0 and 100 to words. I'm not sure which strategy would be bes

Given an array of size n. It contains numbers in the range 1 to n. Each number is present at least once except for 2 numbers. Find the missing numbers. eg. an array of size 5 elements are suppose 3,1,

I am trying to automate a process which at the moment is done by hand. I have several gif images which contain a currency symbol + a numeric value just like this one: I am trying to obtain the numeri

When converting an NSString, which contains standard decimal numbers with two digits (e.g. 8.20) to a NSNumber, I get (from time to time) extra digits and a strange rounding behavior when logging the

For example i want to find numbers including 2,7 from 1 to 7000 without converting string. 2 7 12 17 ... 20 21 22 23...7000 Is there a good algorithm with math? thank you in advance...

Can someone explain why doing an OR operation on a binary number with 0x0030 as the operand produces the ASCII character of that number?

I have the following list and I need to order them from biggest to smallest names and numbers. ['joe: 5', 'ed: 9', 'joe: 7', 'ed: 8'] I'm having the following problems: Converting the numbers from

myCoolObject { a: 0 b: 12 c: 24 } I want to concatenate a, b and c so that they look like a unique string a-b-c (or 0-12-24 in the example). a, b and c will always represent numbers. Converting e

Does anyone know how to add 2 binary numbers, entered as binary, in Java? For example, 1010 + 10 = 1100.

I am in the process of converting non-Spring code into Spring (v3.2) code. I have read through the Spring documentation but I am having trouble wrapping my head around the following situation shown be

I'm using htmlentities which is converting characteres with accents, but it is not converting this type of quotes “. Instead the browser shows a weird symbol with a question mark � How can I convert t

When a user inserts a Persian number in a JTextField, I want it to be persisted in English format. I can handle this issue in Java by converting the numbers manually before persisting. But there are s

this is my first question here, i'm making a little game, and for assigning the roles, i need random numbers. I have this code, but is possible that the numbers don't be repeated? Thanks. void giverol

Is there a nicer way of converting a number to its alphabetic equivalent than this? private String getCharForNumber(int i) { char[] alphabet = ABCDEFGHIJKLMNOPQRSTUVWXYZ.toCharArray(); if (i > 25

I have been working on a random number gen, that creates 6 unique random numbers 1-49, i can get it working so theres 6 random numbers 1-49, or 6 unique random numbers but not all of the above at the

While thinking about this question and conversing with the participants, the idea came up that shuffling a finite set of clearly biased random numbers makes them random because you don't know the orde

My current project involves an address counter entity that outputs a 7-bit vector and that output (m) goes into a sine lookup table entity. So basically, I want m to be the x, and the output of the si

I am new to C. I am experienced in GWBASIC. In an effort to learn, I am attempting to write a program that will convert the individual chars in a string to a numerical value as so: 1 2 3 4 5 6 7 8 9 a

I am converting a project from Seam to CDI and I am quite new in these fields. I am stuck in converting @Startup, @Cretae, @RequestParameter , @Out , @AutoCreate ,@ Destroy tags, what is the alternati

I'm a C++ newb (I'm sure you will be able to tell) and I thought I would try and write some encryption program, that converts input to numbers in a file. I made a table, giving each letter its own num

I have a value in the database of type 'decimal(18,2)' , it has values like 2.50, 1.25 etc.. for some reason when i pull it in, it is not displayedm but all the other values of my table are.. I assume

First of all, for those of you, who don't know (or forgot) about Lychrel numbers, here is an entry from Wikipedia: http://en.wikipedia.org/wiki/Lychrel_number. I want to implement the Lychrel number d

I've starting messing up with javascript lately and stumbled upon some problem. I'm allowing my user to insert a series of numbers separated by white-spaces into a text field. I am trying to read the

Hello I am having trouble converting a long (cents) into currency format. My Code: long doublePayment = 1099; //Should equal $10.99 DecimalFormat dFormat = new DecimalFormat(); String formattedString

This question already has an answer here: How to convert std::string to LPCWSTR in C++ (Unicode) 4 answers I'm having a hard time converting a string into LPWSTR so I can use the PathStripToRoo

Before converting wstring to double - how to validate it with regex? Java no problem, but C++ raising questions.. :)

What are the various advantages/disadvantages from a computer architecture of fixed and floating point numbers? I can understand that both lead to inaccuracy of sorts. My other questions are How do

I wrote this code for converting binary to text . public static void main(String args[]) throws IOException{ BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); System.out.printl

How is the text processed while converting a .doc file to .pdf file.I tried to intercept the Tj operator using Pdfbox. The sentence interchange features of PDF. Again, the resulting PDF file can be

I'm trying to write a code that would convert letters into numbers. For example A ==> 0 B ==> 1 C ==> 2 and so on. Im thinking of writing 26 if statements. I'm wondering if there's a better w

I am Converting a QuickReport to a FastReport in Delphi source, I want to determine the event method name that assigned to a QuickReport object and according to it assign a method to the same event of

This question already has an answer here: JavaScript numbers to Words 10 answers How can I use Javascript for converting numbers to words? The display requires Indian rupees and paise format.

And I would like to program something like, displaying the prime numbers in a listbox if user will input any integer in the textbox. (that means, if they write 10, it will display the prime numbers fr

I know this is a very basic question, but I'm having trouble finding the standard / best way to do this in a rails app. This seems like a pretty typical problem, I'm sure there is an elegant way to so

How do i print consecutive numbers in using a single sql query .. like 1 , 2 , 3 ...

How to compare hexadecimal number with hexadecimal numbers in shell?

I need to write a function which gets two numbers in hexadecimal base, and calculates the sum of both of them, I'm not allowed to convert them to decimal base, the code is supposed to calculate it ma