0

I am trying to get my code to perform a calculation correctly in emu8086, however, I am not sure how to convert a string into an integer in assemly.

  message1 db 0dh, 0ah, "input width: $"
   message2 db 0dh, 0ah, "Input perimeter: $"                     
   width dw ' ', 20 dup('?')
   height dw ' ', 20 dup('?')
   w dw 0
   h dw 0

    calc: 
    mov dx, offset message1
    mov ah, 9
    int 21h  

    lea dx, width
    mov ah, 0ah 
    int 21h 
    mov cx, width

    xor bx, bx
    .next_digit1:
    mov ax, byte[cx]
    inc cx
    sub al, '0'
    mul bx, 10
    add bx, ax
    loop .next_digit1
    mov ax, w     


    mov dx, offset message2
    mov ah, 9
    int 21h 

    lea dx, height
    mov ah, 0ah 
    int 21h 
    mov cx, height

    xor bx, bx
    .next_digit2:
    mov ax, byte[cx]
    inc cx
    sub al, '0'
    imul bx, 10
    add bx, ax
    loop .next_digit2
    mov bx, 2
    div bx
    mov bx, w
    sub bx, ax
    mov ax, h 


    mov cx, w 
    add cx, 100
    mov dx, h
    add dx, 20

I need to convert the input, so that w and h values are integers, so I can perform additions with them and normal integer values, is there a way to make the actual input be concidered integer, or do I have to convert it, and in either case, how would I go about doing it? I am not very experienced with assembly language.

Arnold
  • 33
  • 1
  • 5
  • 11
  • 4
    There are plenty of ready-made functions for that, Google is your friend. For instance if you're linking in an assembly module to a C program you can always call on atoi – doynax Dec 28 '13 at 21:03
  • This has been answered like a thousand times, do some search. – m0skit0 Dec 28 '13 at 21:16
  • See for example http://stackoverflow.com/questions/19309749/nasm-assembly-convert-input-to-integer/19312503#19312503 – Michael Dec 28 '13 at 21:29

2 Answers2

2

If you want to get an Integer from a String in 8086 assembly (and for ex. your command-line parameters are Strings), you must convert the input indeed. It can be done relatively simply. Your input String is actually a table of ASCII signs. If you are sure there are only digits 0-9 in your input (if you are not sure, you need to check that first), just do the following:

  1. Prepare your output variable (may be a register, of course), initialize it to 0.

  2. Multiply your result by 10 ("prepare space" for the comming digit).

  3. Take the first digit (from the left, the most significant digit in your future integer) from your input and substract 48 from it ('0' in ASCII is 48, search for an ASCII table if this seems unclear).

  4. Add the value you got (digit - 48) to your result.

  5. Repeat [2, 3, 4] for all the signs in your input, from left (most significant digit) to right (least significant digit).

Following this simple algorithm you can convert your String input to an Integer output quite simply. There are, of course, many ready-to-use solutions on the Internet; if you decide to use one of them, then the algorith above should hopefully help you understand the code.

Some more research could lead you here: Convert string to int. x86 32 bit Assembler using Nasm.

Community
  • 1
  • 1
3yakuya
  • 2,622
  • 4
  • 25
  • 40
  • Thank you for the explanation, it is a bit clearer, however I am still a bit confused about something, I have updated the code with an example I tried to implement that Michael linked to, however I can't get it to work, if possible, could I get some help in what I'm doing wrong? – Arnold Dec 28 '13 at 22:06
  • it seems emu8086 does not like the movzx ax, byte[dx] line. – Arnold Dec 28 '13 at 22:14
  • I must say I am surprised to see mul working with 2 operands (but my knowledge here may be just out-of-date). Standard mul operation took only one operand and returned the result in AX (if we were multiplying 8-bit values). From my point of view, except from the MUL operation, the code to convert String to Int is more-or-less implemented in the fourth paragraph of your code (starting from `xor bx, bx`) – 3yakuya Dec 28 '13 at 22:51
  • I know this has been a while, but I'm hoping you'll still reply to this. The byte[] and the movzx either don't exist in emu8086, or I don't know how to use them. I would really appreciate some help. – Arnold Dec 29 '13 at 18:58
  • 1
    I believe movzx is not there in emu8086. You can use something like `XOR ax, ax MOV al, 8bitValue` instead (so you will pack your 8 bit value to the lower half while you are sure the upper half is 0 after XOR operation). – 3yakuya Dec 29 '13 at 23:30
  • `movzx` was new in 386. – Peter Cordes Mar 29 '18 at 03:15
-2
in al=keyboard ascii
out al=hexadecimal digit

org 3C8A
call 3cc3   ;string decimal numeric nc=no
jc 3c9f     ;its 0-9
nop 
nop 
nop 
push    ax
or  al,20   ;convert TO LOWER case
sub al,61   ;'a'
cmp al,5    ;'f'-'a'
ja  3ca1    ;not a-f or A-F
add al,a 
inc sp  ;loop 3c9d  normal return (first pop old AX)
inc sp 
clc         ;loop 3c9f
ret 

org 3ca1
pop ax  ;loop 3ca1  error
stc 
ret 

org 3cc3
mov ah,al
sub al,30          ;'0'
jl     3ccf
cmp al,9           ;9
ja  3ccf        ;if not digit
stc
ret
mov al,ah   ;3ccf loop    not number
clc      
ret
taskinoor
  • 45,586
  • 12
  • 116
  • 142
patl
  • 1
  • 1