Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: programming
I was working on some Assembly code, and I need to separate the digits of my WORD variable (12345). I've done this, and I am unsure as to how to loop this:
 
org 100h 
 
section .text 
 
start: 
    mov bx, output  ; put address of output into BX
    mov ax, [num]   ; put 16-bit value stored at num in AX
    sub dx, dx      ; let DX = 0
 
    mov cx, 10000   ; Divide AX by 10000
    div cx          ; result in AX, remainder in DX
    add al, 30h     ; ASCII '1' = 49 (or 31h) so add 30h
    mov [bx], al    ; Store ASCII char in output
    inc bx          ; point to next char in output
    mov ax, dx      ; move remainder of last division into AX
    sub dx, dx      ; clear remainder
 
    mov cx, 1000    ; Divide AX by 1000
    div cx
    add al, 30h
    mov [bx], al
    inc bx
    mov ax, dx
    sub dx, dx
 
    mov cx, 100     ; Divide AX by 100
    div cx
    add al, 30h
    mov [bx], al
    inc bx
    mov ax, dx
    sub dx, dx
 
    mov cx, 10      ; Divide AX by 10
    div cx
    add al, 30h
    mov [bx], al
    inc bx
    mov ax, dx
    sub dx, dx
 
    mov cx, 1
    div cx
    add al, 30h
    mov [bx], al
   
    mov bx, output  ; get address of first char in ouput
 
myloop:
    mov dl, [bx]    ; get char at address in BX 
    inc bx          ; point BX to next char in message
    cmp dl, 0       ; Is DL = null (that is, 0)?
    je  quit        ; If yes, then quit
    mov ah, 06      ; If no, then call int 21h service 06h
    int 21h         ;    to print the character 
    jmp myloop      ; Repeat for the next character
 
quit:
    ; DONE!
    int 20h
 
section .data 
    num dw  12345
    output  db  0,0,0,0,0,0
Posted 8-Nov-12 15:44pm
Edited 8-Nov-12 20:27pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Instead of dividing by 10000, then 1000, then 100,... the more normal way is to divide by 10 each time and output the bytes backwards - least significant first. This way, you can cope with numbers more flexibly. You then return a pointer to the most significant byte and only loop while you have digits left to do. I.e. your loop termination test (done after doing each digit) is AX equals zero. Doing the test after each digit means you will always output at least one digit - so a zero input causes a single digit zero output.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 499
1 Nirav Prabtani 298
2 OriginalGriff 215
3 _Amy 165
4 Abhinav S 160
0 OriginalGriff 7,715
1 Sergey Alexandrovich Kryukov 6,911
2 Maciej Los 3,954
3 Peter Leow 3,698
4 CHill60 2,742


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100