HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Assembly 8086 program to input a 16-bit signed number (in string form) and output its binary equivalent

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
bitnumberequivalentandprogramoutputinput8086assemblyits

Problem

I'm working on an exercise using Assembly 8086 which inputs a number (in string form) then output the binary form. Here is what I did (tested on emu8086 by the way):

-
Convert the string to numeric form: I simply iterated through the string, and for each new value I simply multiply the old value by 10, then add by the new value. But there are some checks for overflow which was annoying: I have to check in both the multiplication and addition part, and since in this part I simply ignored the negative part (for simplicity) I had to check for a special case: a 16-bit signed number goes from \$-2^{15} = -32768\$ to \$2^{15} - 1 = 32767\$, so if the input is -32768 then there would be an error, so I had to check for that case specifically.

-
From numeric to binary: I simply left-shifted the number (which is now stored in AX) consecutively and printed the most significant bit.

Please give some feedback, because I'm not sure if the overflow part is ok.

```
;Written by Dang Manh Truong
.stack 100h
.data
base10_string dw "-32799$"
biggest_16bits_signed equ 7FFFh
special_16bits_signed equ 8000h
special_16bits_signed_str dw "1000000000000000$"
error_overflow dw "Arithmetic overflow encountered. Abort$"
error_not_a_number dw "Not a number. Abort$"
base10 equ 10
tmp dw 0
is_negative dw 0
.code
main proc
;Initialization
mov ax,@data
mov ds,ax

;;;part 1: convert string to value
mov ax,0 ;number = 0;
lea si,base10_string
;check if positive or negative
mov bl,[si] ;bl = value
cmp bl,'+'
jne check_if_negative
add si,1 ;start from after the "+" sign
mov ax,0
jmp while_loop
check_if_negative:
cmp bl,'-'
jne check_if_not_a_number
mov ax,1
mov is_negative,ax
mov ax,0
add si,1 ;start from after the "-" sign
jmp while_loo

Solution

One of the most problematic things to deal with in assembly and even other higher languages but to a lesser degree, is spaghetti code. The logic of the program becomes lost in scrolling back and forth through hundreds of lines, SO concise documentation of what is going in is essential.

Working from the inside out is a method that has worked very effectively for me.

; Read ASCII NULL terminated string and convert input to packed BCD.

      GetNext:    lodsb                  ; Read next character into AL
                  or      al, al         ; Teminator ?
                  jz      Done

                 .......

                 jmp     GetNext

       Done:


Sometimes you may even want to think of efficiency in terms of time. The most probable scenario is that you'd be encountering digits 0 to 9.

GetNext:    lodsb                  ; Read next character into AL
                  cmp     al, '0'
                  jae     @F
                  cmp     al, '9'
                  jbe     @F
                  cmp     al, 0
                  jz      Done

    ; At this point we know our entry is invalid or alternately we might
    ; want to check for things like '+', '-' or in this case just ignore
    ; extraneous input.

                 jmp     GetNext

            @@:  ; This is peculiar to MASM and is a local label.
                 ; NASM it could be preceded with a period like .L0:

                 .......

                 jmp     GetNext

       Done:

Code Snippets

; Read ASCII NULL terminated string and convert input to packed BCD.

      GetNext:    lodsb                  ; Read next character into AL
                  or      al, al         ; Teminator ?
                  jz      Done

                 .......

                 jmp     GetNext

       Done:
GetNext:    lodsb                  ; Read next character into AL
                  cmp     al, '0'
                  jae     @F
                  cmp     al, '9'
                  jbe     @F
                  cmp     al, 0
                  jz      Done

    ; At this point we know our entry is invalid or alternately we might
    ; want to check for things like '+', '-' or in this case just ignore
    ; extraneous input.

                 jmp     GetNext

            @@:  ; This is peculiar to MASM and is a local label.
                 ; NASM it could be preceded with a period like .L0:

                 .......

                 jmp     GetNext

       Done:

Context

StackExchange Code Review Q#158103, answer score: 3

Revisions (0)

No revisions yet.