存储和字符串处理的x86汇编语言 [英] Storage and String manipulation in x86 Assembly Language

查看:230
本文介绍了存储和字符串处理的x86汇编语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚拿起学习汇编语言代码。

问:转换的字符串,再presents任何符号整数其补值,与存储在小尾数顺序存储连续位置的结果

例如 - 1 = 0xFFFFFFFFFFFFFFFE假设2的补codewards都是64位。我已经做了数-149在我的code这将导致
 为0xFFFF FFFF FFFF FF6B

 。数据
小号:.string-149
结果:.quad            。文本
            .globl主主要:
    MOV S,RAX%
    CMP%RAX,0
    JL积极
    子%RAX,RAX%
    不发
    加S,RAX%
    子$ 30%RAX
    不RAX%
    加$ 1,%RAX
    MOV%RAX,结果正面的:
    子$ 30%RAX
    不RAX%
    加$ 1,%RAX
    MOV%RAX,结果

在GDB,对于存储在字符串整数值是这样的。

 (GDB)X / 24xb&安培; S
0x601038:0x2d 0X31 0x34 0x39为0x00为0x00为0x00为0x00
0x601040:0×00 0×00 0×00 0×00 0×00 0×00 0×00 0×00
0x601048:0×00 0×00 0×00 0×00 0×00 0×00 0×00 0×00

如果我想要做的任何计算为-149,我不得不以某种方式访问​​存储这些位置 - 我怎么去这样做。

如果我知道,4是10的地方,我可以乘以10就得到40,然后添加9 similiar 1×得到100,并添加为好。

我如何访问它们做计算?


解决方案

  

我如何访问它们做计算?


的字符串被存储作为在存储器连续字符。如果它是ASCII(不是UTF-8),每个字符是单字节。

所以,你可以用字节加载/存储,如 movzbl 2(%RSI),%EAX A的时间才能拿到第三个字符访问它们之一,如果 RSI 指向字符串的开始。

或者,如果%RDI 指向最后一个字符(那些放置在一个十进制数),那么 IMUL $ 10 -1(% RDI),ECX%将设置 CL%来倒数第二个字符加上其位值。 (和高位字节%ECX 的垃圾,它可能会更好先做一个 MOVZX 负荷再乘这<一个href=\"http://stackoverflow.com/questions/34377711/which-2s-complement-integer-operations-can-be-used-without-zeroing-high-bits-in\">does工作,虽然,以获得低8位正确)。

在复杂光谱的另一端,看看这个 SSE4.1 IPv4点分四串32位整数转换器。具体来说,洗牌后的小数位值的部分,使用 pmaddubsw _mm_maddubs_epi16 )用一个vector [...,100,10,1] 应用位值和水平增加,那么 phaddw 来一步水平方向每点分四组增加了对三位。

此外如何使用SIMD实现的atoi?

又见 86 标记维基地段其他链接。

I've just picked up learning assembly language coding.

Q: Convert a character string that represents any signed integer to its 2’s complement value, with the result stored in consecutive locations of memory in little endian order.

For example - 1 = 0xFFFFFFFFFFFFFFFE assuming 2's complement codewards are 64-bit. I've done the number -149 in my code which should result in 0xffff ffff ffff ff6b

            .data
S:  .string "-149"
Result:     .quad

            .text
            .globl main

main: 
    mov     S,%rax
    cmp     %rax,0
    jl      positive
    sub     %rax,%rax
    not     S
    add     S,%rax
    sub     $30,%rax
    not     %rax
    add     $1, %rax
    mov     %rax,Result

positive:
    sub     $30,%rax
    not     %rax
    add     $1,%rax 
    mov     %rax,Result

In GDB, the value for the string integer stored is this.

(gdb) x/24xb &S
0x601038:   0x2d    0x31    0x34    0x39    0x00    0x00    0x00    0x00
0x601040:   0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00
0x601048:   0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00

if I wanted to do any computations to -149, I'd have to somehow access these locations in the memory - how do I go about doing this?

If I know that the 4 is in the 10's place, I could multiply it by 10 to get 40 and then add the 9 and similiar 1x100 to get 100 and add that as well.

How do I access them to do the computation?

解决方案

How do I access them to do the computation?

A string is stored as consecutive characters in memory. If it's ASCII (not UTF-8), each character is a single byte.

So you can access them one at a time with byte loads/stores, like movzbl 2(%rsi), %eax to get the 3rd character, if rsi points to the start of the string.

Or, if %rdi points to the last character (the ones place in a decimal number), then imul $10, -1(%rdi), %ecx will set %cl to the second-last character plus its place-value. (And the upper bytes of %ecx to garbage; it's probably better to do a movzx load first and then a multiply. This does work, though, to get the low 8 bits correct).

At the other end of the complexity spectrum, have a look at this SSE4.1 IPv4 dotted-quad string to 32bit integer converter. Specifically, the decimal place-value part after the shuffle, using pmaddubsw (_mm_maddubs_epi16) with a vector of [ ..., 100, 10, 1 ] to apply the place-value and one step of horizontal adding, then phaddw to horizontally add the up-to-three digits from each dotted quad.

Also How to implement atoi using SIMD?

See also the tag wiki for lots of other links.

这篇关于存储和字符串处理的x86汇编语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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