[英]Two digit string number Assembly
因此,我必须对字符串s1和s2进行字符串处理,并且我有两个字符串d,其中包含s1和s2每个位置的最大值。
例如:
S1: 1, 3, 6, 2, 3, 10
S2: 6, 3, 11, 1, 2, 5
D: 6, 3, 11, 2, 3, 10
所以这是代码
bits 32
global start
extern exit,printf
import exit msvcrt.dll
import printf msvcrt.dll
segment data use32 class=data
format db "%s",0
s1 db "1","3","6","2","3","10"
l equ $-s1
s2 db "6","3" ,"11","1","2", "5"
d times l db 0
segment code use32 class=code
start:
mov esi,0
mov edi,0
cld
Repeta:
mov al,[s1+esi]
mov bl,[s2+esi]
cmp al,bl
jg et1
mov[d+edi],bl
inc edi
inc esi
jmp et2
et1:
mov[d+edi],al
inc edi
inc esi
et2:
cmp esi,l
jne Repeta
push d
push format
call[printf]
add esp,4*2
push dword 0
call [exit]
问题在于,当到达两位数的元素(10或11)时,它仅获取第一个数字(1),并将其与相同位置上另一字符串的数字进行比较,之后获取第二个数字并对其进行比较以及其他字符串中的下一个数字。 我该如何解决?
它说应该是一个字节字符串
短语“ of of bytes”对我而言非常强烈地暗示着数组。 请您的讲师进行澄清,但我认为s1: db 1, 3, 6, 2, 3, 10
您应该使用s1: db 1, 3, 6, 2, 3, 10
,因此元素是固定宽度的单字节整数。 (而不是ASCII字符串)。
这意味着您可以使用简单的成对最大值,例如SSE2 pmaxub
(用于无符号字节)或SSE4.1 pmaxsb
(用于有符号字节)。
segment data use32 class=data
format db "%s",0
s1 db 1, 3, 6, 2, 3, 10
l equ $-s1
s2 db 6, 3, 11, 1, 2, 5
d times l db 0
start:
movq xmm1, [s1] ; load all 6 elements, plus 2 bytes past the end but that's ok. We ignore those bytes
movq xmm2, [s2]
pmaxub xmm1, xmm2 ; element-wise vertical max
;but avoid storing outside of 6-byte d
movd [d], xmm1 ; store first 4 bytes of the result
pextrw [d+4], xmm1, 2 ; store bytes 4 and 5 (word 2 of xmm1 = 3rd word)
... ; the result isn't a string, you can't print it with printf.
对于不是2的倍数的字节计数,例如,如果l
为7,则可以使用它代替pextrw
:
psrldq xmm1, 3 ; bring the data you want to store down into the low 4 bytes of the register
movd [d+4], xmm1 ; 4-byte store that overlaps by 1
顺便说一句,我意识到您打算一次循环1个字节的元素。 如果cl > al
(带符号),则可能使用cmp cl, al
/ cmovg eax, ecx
/ mov [edi], al
来存储原来在cl
,否则要存储最初在al
。
我认为您的循环结构有些破损,因为您有一个路径不存储到d
。 无论哪个来源更大,您总是需要存储到d
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.