8086 assembly convert input string to integer

混江龙づ霸主 提交于 2019-12-13 06:04:55

问题


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.


回答1:


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.




回答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


来源:https://stackoverflow.com/questions/20819206/8086-assembly-convert-input-string-to-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!