0

In MASM Assembly, if the user writes "-22", I want to check the first character for a "-" sign and then drop it. If the user writes "+12", I want to check the first character for a "+" sign and then drop it.

The code below is not working. Could someone please let me know what is wrong with the code.

insertNums:

    lodsb

    cmp     ch, 43  ;I am not sure if this is the proper way of checking for a "+"
    je      convertPos
    cmp     ch, 45  ;Same with this. Does this check for ASCII 45 which is "-"?
    je      convertNeg
continue:
    cmp     eax, 0
    je      endInsert
    sub     eax, 48
    mov     ebx, eax
    mov     eax, value
    mov     edx, 10 ;multiplies by 10
    imul    edx
    jc      tooLargeNumber ; check for carry after multiply
    add     eax, ebx ;add the digit to the converted value
    jc      tooLargeNumber ; check for carry after adding
    mov     value, eax ; store temporary value from eax
    mov     eax, 0 ; reset eax
    loop    insertNums
    jmp     endInsert


convertPos:
; this is for positive numbers
; the goal is to just drop the'+' sign and feed it back to the loop

convertNeg:
; this one is supposed to drop the '-' sign
; and once I know it is a negative number, I would have a separate procedure for ;converting the number to a negative
phuclv
  • 37,963
  • 15
  • 156
  • 475
halpme
  • 17
  • 3
  • 2
    `cmp ch, 43` <-- If we assume that the `lodsb` just before this is what you use to load each character, then the character will be in `al`, not `ch`. Also, there's no point in using ASCII codes like 43 and 45 directly; just use `'+'` and `'-'`. – Michael Mar 15 '20 at 08:22
  • Thanks for the explanation, Michael. How would I drop the '+' or '-' signs? Would it work to just replace those characters with 0? Edit: Nvm. Replacing them with 0's didn't work. The procedure just thinks it's done inserting numbers and jumps to endInsert. – halpme Mar 15 '20 at 18:01
  • Just load the next character and enter the digit-loop the same way you would have if you'd started with a digit. – Peter Cordes Mar 15 '20 at 18:48

0 Answers0