本文介绍了NASM大会将输入到整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我是相当新的装配,事实上,我是很新的装配。我写了一张code这只是意味着把数字输入从用户,乘以10的,并有pssed通过程序退出状态的用户,结果前$ P $的(通过键入回声$?在终奌站)
问题是,它没有给予正确的数字,4×10表现为144.所以后来我想通了投入很可能是为一个字符,而不是一个整数。在这里我的问题是,我怎么字符输入转换为整数,这样就可以在算术计算中使用?

这将是巨大的,如果有人能回答记住我是一个初学者:)
另外,我怎么能转换成整数说回一个字符?

 部分。数据.bss段
输入RESB 4.text段全球_start
_开始:MOV EAX,3
MOV EBX,0
MOV ECX,输入
MOV EDX,4
INT 0x80的MOV EBX,10
IMUL EBX,ECXMOV EAX,1
INT 0x80的


解决方案

下面是字符串转换为整数,一对夫妇的功能,反之亦然:

 ;输入:
; ESI =指向字符串转换
; ECX =字符串中位数(必须大于0)
;输出:
; EAX =整数值
string_to_int:
  XOR EBX,EBX;明确EBX
.next_digit:
  MOVZX EAX,字节[ESI]
  INC ESI
  子等,'0';从ASCII到数字转换
  IMUL EBX,10
  添加EBX,EAX; EBX = EBX * 10 + EAX
  环.next_digit;而(--ecx)
  MOV EAX,EBX
  RET
;输入:
; EAX =整数值转换
; ESI =指针缓冲存储在字符串(必须有房间至少10字节)
;输出:
; EAX =指针到生成的字符串的第一个字符
int_to_string:
  加ESI,9
  MOV字节[ESI],STRING_TERMINATOR  MOV EBX,10
.next_digit:
  XOR EDX,EDX;分EDX之前清除EDX:由EBX EAX
  DIV EBX; EAX / = 10
  添加DL,'0';转换其余为ASCII
  十二月ESI;以相反的顺序存储的字符
  MOV [ESI],DL
  TEST EAX,EAX
  JNZ .next_digit;重复,直到EAX == 0
  MOV EAX,ESI
  RET

这是你如何使用它们:

  STRING_TERMINATOR EQU 0LEA ESI,[thestring]
MOV ECX,4
电话string_to_int
; EAX现在包含1234;将其转换回一个字符串
LEA ESI,[缓冲区]
调用int_to_string
;你现在有EAX,一个字符串指针,
;你可以用SYS_WRITE系统调用使用thestring:DB1234,0
缓冲区:10 RESB

请注意,我没有做太多的错误,这些例程检查(如检查是否有范围'0'以外的字符 - 9)。也不例程处理有符号数。所以,如果你需要这些东西你必须给他们自己添加。

Ok, so I'm fairly new to assembly, infact, I'm very new to assembly. I wrote a piece of code which is simply meant to take numerical input from the user, multiply it by 10, and have the result expressed to the user via the programs exit status (by typing echo $? in terminal)Problem is, it is not giving the correct number, 4x10 showed as 144. So then I figured the input would probably be as a character, rather than an integer. My question here is, how do I convert the character input to an integer so that it can be used in arithmetic calculations?

It would be great if someone could answer keeping in mind that I'm a beginner :)Also, how can I convert said integer back to a character?

section .data

section .bss
input resb 4

section .text

global _start
_start:

mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, 4
int 0x80

mov ebx, 10
imul ebx, ecx

mov eax, 1
int 0x80
解决方案

Here's a couple of functions for converting strings to integers, and vice versa:

; Input:
; ESI = pointer to the string to convert
; ECX = number of digits in the string (must be > 0)
; Output:
; EAX = integer value
string_to_int:
  xor ebx,ebx    ; clear ebx
.next_digit:
  movzx eax,byte[esi]
  inc esi
  sub al,'0'    ; convert from ASCII to number
  imul ebx,10
  add ebx,eax   ; ebx = ebx*10 + eax
  loop .next_digit  ; while (--ecx)
  mov eax,ebx
  ret


; Input:
; EAX = integer value to convert
; ESI = pointer to buffer to store the string in (must have room for at least 10 bytes)
; Output:
; EAX = pointer to the first character of the generated string
int_to_string:
  add esi,9
  mov byte [esi],STRING_TERMINATOR

  mov ebx,10
.next_digit:
  xor edx,edx         ; Clear edx prior to dividing edx:eax by ebx
  div ebx             ; eax /= 10
  add dl,'0'          ; Convert the remainder to ASCII
  dec esi             ; store characters in reverse order
  mov [esi],dl
  test eax,eax
  jnz .next_digit     ; Repeat until eax==0
  mov eax,esi
  ret

And this is how you'd use them:

STRING_TERMINATOR equ 0

lea esi,[thestring]
mov ecx,4
call string_to_int
; EAX now contains 1234

; Convert it back to a string
lea esi,[buffer]
call int_to_string
; You now have a string pointer in EAX, which
; you can use with the sys_write system call

thestring: db "1234",0
buffer: resb 10

Note that I don't do much error checking in these routines (like checking if there are characters outside of the range '0' - '9'). Nor do the routines handle signed numbers. So if you need those things you'll have to add them yourself.

这篇关于NASM大会将输入到整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:37
查看更多