8086汇编转换输入字符串到整数 [英] 8086 assembly convert input string to integer

查看:522
本文介绍了8086汇编转换输入字符串到整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的code在EMU8086正确地进行计算,但是,我不知道如何将字符串转换成assemly一个整数。

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

我需要将输入转换,使W和H值是整数,所以我可以执行与他们和正常的整数加法,有没有一种方法,使实际的输入来concidered整数,或者我需要转换它,并在任何情况下,我怎么会去这样做呢?我不是很有经验的汇编语言。

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.

推荐答案

如果您想从一个字符串中的一个整数8086组件(和前。您的命令行参数字符串),你的确应该将输入。它可以相对简单地进行。
您的输入字符串实际上是ASCII迹象表。如果您确信,只有数字0-9在输入(如果你不知道,你需要检查第一),只需做到以下几点:

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您的输出变量(可以是一个寄存器,当然),它初始化为0。

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

10乘以你的结果(prepare空间正在添加的数字)。

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

在第一个数字(从左边,在你未来的整数最显著位)从输入。减去48从它('0'的ASCII码为48,寻找一个ASCII表,如果这似乎不清楚)。

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).

你得到的数值(数字 - 48)添加。您的结果

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

重复[2,3,4]在您输入的所有迹象,从左(最显著位)到右(最显著位)。

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

根据这一简单的算法您可以将输入的字符串转换为整数输出很简单。有,当然,许多因特网上现成的使用的解决方案;如果你决定使用其中的一种,那么上面的algorith应该有希望帮助您了解code。

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.

一些更多的研究可以在这里带领您:<一href=\"http://stackoverflow.com/questions/19461476/convert-string-to-int-x86-32-bit-assembler-using-nasm\">Convert字符串为int。 86的32位汇编​​使用NASM 。

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

这篇关于8086汇编转换输入字符串到整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆