assembly - converts decimal number to its binary form in nibbles -


here's code, program should ask number between 0-65,535 , should output decimal form in nibbles

.model small .data prompt db "input integer in interval [0, 65535]: $", 0 number dw 0h digitcounter db 0h place_value dw 0h number_of_bits dw 0h 2 db 2  .stack 4096 .code readint proc mov ah, 01h ; reads character keybaord , loads al register int 21h ret  readint endp ; after readint, al has integer itself, , ax has 0001 xxxx  newline proc mov dl, 10 mov ah, 02h int 21h mov dl, 13 mov ah, 02h int 21h ret newline endp  main proc  ; clearing data mov ax, @data mov ds, ax  ; start of main() proper call newline mov dx, offset prompt mov ah, 09h int 21h   dloop: cmp digitcounter, 05h ; if have read 6 digits je prog_cont ; jump call readint ; puts read character ah register       cmp al, 0dh  je prog_cont  sub al, 30h ; after point, read character sure integer ; subtract 30h actual value mov ah, 0h ; makes higher bits of ah 0 push ax ; pushes integer stack inc digitcounter jmp dloop passed_ones: dec digitcounter add number, cx; ; adds special case (the ones digit), number cmp digitcounter, 0h je ones_cont inc place_value ; move next place value of number (tens) prog_cont: pop cx ; pops topmost element of stack register cx cmp place_value, 0h ; @ beginning of iteration known ones digit je passed_ones mov ax, 10 ; first take place value multiplier, e.g., tens - 10, hundreds - 100 mul place_value ; result stored in dx:ax augmented register mov bx, ax ; move result register because use ax again multiplication mov ax, cx ; move popped value cx register ax register mul bx ; ax = contains digit, bx = contains place value, dx:ax - result add number, ax ; add result number  mov ax, 0ah ; first take place value multiplier, e.g., tens - 10, hundreds - 100 mul place_value mov place_value, ax  dec digitcounter jnz prog_cont  ; after point, variable number contains number  ones_cont:   mov ax, number  do: div 2 ; after division, remainder (the digit want) stored in ah register mov cl, ah ; copy ah bh can use ax again push cx ; stack has digits of number in reverse order, when pop ; first value pop value of largest place value inc number_of_bits mov ah, 0h ; clear out ah because ax used again division cmp al, 01h ; continue dividing quotient, stored in al , if it's zero, move on jne  continue: inc number_of_bits mov cx, 01h push cx  mov ax, number_of_bits jmp evening_out_dem_bits append_zero: mov bl, 0h push bx ; pushes value '0' stack inc number_of_bits mov ax, number_of_bits evening_out_dem_bits: , ax, 03h ; use bit masking last 2 bits of  ; binary number, , if both of them zero, know ; number divisible 4 ; answer, after logical operation, stored in first operand cmp ax, 0h  jne append_zero  ; after point, of zeros ready printed console call newline jmp printing_dem_bits  decrease_num_bits: dec number_of_bits printing_dem_bits: pop cx ; pops significant bit , loads register dx ; dh has bit want print, add cx, 30h ; convert number it's ascii form printing mov dx, cx mov ah, 02h int 21h  cmp number_of_bits, 0h jne decrease_num_bits  ; end of main() proper  ; returning ms-dos mov ax, 4c00h int 21h  main endp end main 

whenever input numbers below 512 displays correctly when input number greater or equal 512 has infinite loop. i'm newbie here please help. thank you

div two performs 'al = ax / two'. if result won't fit al (>255), 'divide overflow error'. case, if ax > 511. change division word-division.

replace

... .data ... 2 db 2 ... .code ... do: div 2 ; after division, remainder (the digit want) stored in ah register mov cl, ah ; copy ah bh can use ax again push cx ; stack has digits of number in reverse order, when pop ; first value pop value of largest place value inc number_of_bits mov ah, 0h ; clear out ah because ax used again division cmp al, 01h ; continue dividing quotient, stored in al , if it's zero, move on jne ... 

by

... .data ... 2 dw 2      ; word causes word-division ... .code ... do: xor dx, dx div 2         ; dx:ax / 2 -> ax, remainder dx mov cx, dx push cx ; stack has digits of number in reverse order, when pop ; first value pop value of largest place value inc number_of_bits cmp ax, 01h jne ... 

there issue elsewhere: 512d not 0010000000001b.


Comments