0

I am in the process of writing a byte sized poly derivative app in MASM x8086 and it has to be able to receive negative coefficients.

I understand that binary can be represented in signed and unsigned form. But I am looking for a way to receive a signed integer so that I can avoid another array. Or is there a way to define my variable as a signed integer?

Below is my integer input procedure.

TEN db 10  ;;; constant
num db ?   ;;; coefficient
           ;;; bh is the degree

get_int PROC
  lea si, string     ; replaces the ? in the string with the degree
  add si, 13h
  mov dl, bh
  add dl, 30h
  mov [si], dl

  mov ah, 09h
  lea dx, string     ; prompt
  int 21h

  mov ah, 0Ah
  lea dx, buffString ; user input
  int 21h

  lea si, buffString ; point to count byte
  inc si

  mov ch, 00h        ; cx = count
  mov cl, [si]

  add si, cx         ; si points to end of string

  mov dl, 00h        ; hold result(dl)
  mov bl, 01h        ; hold 10^x (bl)

loop1:
  mov al, [si]       ; prep for char ---> number conversion
  cmp al, '-'
  je negativeSign

  sub al, 30h        ; convert
  mul bl             ; ax = al*bl
  add dl, al         ; sum of results

  mov al, bl         ; preload for instruction
  mul TEN            ; TEN is variable predefined as 10
  mov bl, al
  jmp overNegative
negativeSign:

  mov dh, 00h
  mov [si], dh
overNegative: 
  dec si
  loop loop1         ; loop instruction uses cx as index counter once zero breaks

  mov num, dl
  ret 
get_int ENDP
; output is num
zx485
  • 28,498
  • 28
  • 50
  • 59
TheLiquor
  • 71
  • 7
  • What exactly is the `c++` part of this? – Simon Kraemer Nov 29 '16 at 16:55
  • removed the tag. there is no c++. – TheLiquor Nov 29 '16 at 16:56
  • Where do you handle '-' char? Do you use "twos complement"? – Ripi2 Nov 29 '16 at 17:11
  • I just added my test for the '-' char into the code provided and now I can handle negative input with the correct output without the negative sign. I understand what twos complement is but am not sure how to apply it. Would it best to apply the signed flag within my if else statement and then test that flag before output? – TheLiquor Nov 29 '16 at 17:14
  • http://ccm.net/contents/62-representation-of-real-numbers-and-integers – Ripi2 Nov 29 '16 at 18:28
  • 2
    I don't understand the question. You claim to know that there is no difference between the binary representation of signed and unsigned integers. That is correct. If you interpret an integer value as "signed", then the high bit is the sign bit: on if it's negative, off if it's positive. Otherwise, if you interpret an integer value as "unsigned", the high bit is just another bit to store numerical data, increasing its total range. So it makes absolutely no sense to ask about "receiving" a signed or unsigned integer in assembly language. It only matters how you interpret the specified parameter. – Cody Gray - on strike Nov 29 '16 at 22:23

1 Answers1

1

When, while interpreting the input, you stumble upon the "-" character it is a save assumption that you reached the start of the number. Therefore you should bail out of the loop. I don't see any point in replacing the "-" character by a zero!

What you do need to do is negate the number to obtain the correct signed result:

loop1:
 mov al, [si]       ; prep for char ---> number conversion
 cmp al, '-'
 je negativeSign

 sub al, 30h        ; convert
 mul bl             ; ax = al*bl
 add dl, al         ; sum of results

 mov al, bl         ; preload for instruction
 mul TEN            ; TEN is variable predefined as 10
 mov bl, al
 dec si
 loop loop1         ; loop instruction uses cx as index counter once zero breaks

 mov num, dl        ;Positive number [0,127]
 ret 

negativeSign:
 mov dh, 00h        <<<<<<< Need this as a flag?
 neg dl
 mov num, dl        ;Negative number [-128,-1]
 ret
Fifoernik
  • 9,779
  • 1
  • 21
  • 27
  • A much better algorithm is `total = total*10 + digit`; that only requires one multiply inside the loop. See [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057) for an x86-64 example (where we can even use LEA to shift and add, avoiding even needing a separate imul instruction). But yeah, handling a leading `-` around an unsigned conversion is the way to go. – Peter Cordes Mar 11 '22 at 15:58