两位数的字符串编号 [英] Two digit string number Assembly
问题描述
所以我必须对字符串s1和s2进行字符串处理,并且我有两个字符串d,其中包含s1和s2的每个位置的最大值.
So I have to strings s1 and s2 and I have two obtain the string d that contains the maximum numbers for each of the positions of s1 and 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),并将其与相同位置上另一字符串中的数字进行比较,之后获取第二个数字并将其与另一个字符串中的下一个数字进行比较. 我该如何解决?
The problem is that when it reaches a double digit element(10 or 11) it takes only the first digit(1) and compares it with the number from the other string on the same position and after that it takes the second digit and compares it with the next number from the other string. How can I solve this?
推荐答案
它说应该是一个字节字符串
it says that it should be a string of bytes
短语"of bytes" 非常强烈地暗示着数组.请教您的讲解者,但我认为s1: db 1, 3, 6, 2, 3, 10
是您应该使用的内容,因此元素是固定宽度的单字节整数. (而不是ASCII字符串).
The phrase "of bytes" very strongly implies array to me. Ask your instructor for clarification, but I think s1: db 1, 3, 6, 2, 3, 10
is what you're supposed to be working with, so the elements are fixed width single byte integers. (And not ASCII strings at all).
这意味着您可以使用简单的成对最大值,例如 SSE2 pmaxub
(用于未签名字节)或 SSE4.1 pmaxsb
(用于签名字节).
This means you can use a simple pairwise max like SSE2 pmaxub
(for unsigned bytes) or SSE4.1 pmaxsb
(for signed bytes).
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
:
For byte counts that aren't a multiple of 2, e.g. if l
was 7, you could use this instead of 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
中的内容.
BTW, I realize that you're intended to loop over the elements 1 byte at a time. Maybe use cmp cl, al
/ cmovg eax, ecx
/ mov [edi], al
to store what was originally in cl
if cl > al
(signed), otherwise store what was originally in al
.
我认为您的循环结构有点混乱,因为您有一个路径不存储到d
.无论哪个来源更大,您总是需要存储到d
.
I think your loop structure is a bit broken, because you have one path that doesn't store to d
. You always need to store to d
, regardless of which source was greater.
这篇关于两位数的字符串编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!