简体   繁体   English

我如何才能更高效地编写此代码?

[英]How can I write this code more efficient?

So one of the problems that were in the exam for me is to make this group of code more efficient with at least 1 line less command. 因此,对我而言,考试中的问题之一是使用至少少一行的命令来提高这组代码的效率。 And I didn't know how to do it.The goal of this code is to get the 4 right bits of the first number,the left bits from the second number from the array (which its address is located in si) and then to merge those 8 bits- and to put the result in a 8 bit register. 我不知道该怎么做。此代码的目标是从数组(其地址位于si中)获取第二个数字的第四个右位,然后从第二个数字获取左个位。合并这8位-并将结果放入8位寄存器中。 0dh is the ASCII of enter,I need to make sure that enter isn't one of the chars that user had input,if it does 0 should replace it.(The array is an array of characters) This is the code: 0dh是enter的ASCII码,我需要确保enter不是用户输入的字符之一,如果输入为0,则应该替换它。(该数组是一个字符数组)这是代码:

I thought maybe she just meant to get the line that doesn't effect the returned value to outside of the function but she told me that that's not what she meant so its wrong. 我以为也许她只是想让那行不影响返回值到函数的外部,但是她告诉我那不是她的意思,所以这是错误的。

cmp [byte ptr si],0Dh
je LessThan4
mov al,[si]
shl al,4;(a)-first nibble
inc si
cmp [byte ptr si],0Dh
je LessThan4
mov dl,[si]
and dl,240;11110000b
shr dl,4;(b)-second nibble 
or al,dl;al=ab merging the nibbles
inc si
jmp Normal
LessThan4:
mov[byte ptr si],0
Normal:
ret

The excepted result is using 1 command,that will swap 2 commands in the current code. 例外结果是使用1条命令,它将在当前代码中交换2条命令。 Edit: Honestly I don't know why did I use this line: mov[byte ptr si],0 I don't need it,I need to put 0 instead of enter if there is enter. 编辑:老实说,我不知道为什么要使用这一行:mov [byte ptr si],0我不需要它,如果有回车,我需要放0而不是回车。 But this is happening alone because the function ends if there is an enter in the array,and 0 is what replacing the second nibble or both of the nibbles but I did need to make sure al is 0. If this is what she meant im so embarrassed and tilted because I might not be able to get to the subject I want to learn next year in our class.): ): ): ): ): I should have been able to see it pretty easily so that's really bad for me... 但这是单独发生的,因为如果数组中有一个enter函数就会结束,并且0是替换第二个半字节或两个半字节的内容,但是我确实需要确保al为0。如果这是她的意思,那么im尴尬和倾斜,因为我可能无法在明年的课程中上到我想学的科目。):):):):):我应该能够很容易地看到它,所以这对我来说真的很糟糕...

Try this then: 然后尝试以下方法:

        lodsw                ; load ax from [si], then increment si twice
        cmp al, 0dh          ; was the first character a CR?
        jz enter1            ; if yes, abort
        cmp ah, 0dh          ; was the second character a CR?
        jz enter2            ; if yes, abort
        rol ax, 4            ; combine the digits in al and ah into al
        ret

enter1: dec si               ; return back to the first character
enter2: dec si               ; return back to the second character
        mov [byte ptr si], 0 ; terminate input with a NUL
        ret

This implementation uses 11 instructions instead of the 15 instruction of your implementation, saving 4 instructions. 此实现使用11条指令而不是您的实现的15条指令,从而节省了4条指令。

The expected result is using 1 command, that will swap 2 commands in the current code. 预期的结果是使用1条命令,它将在当前代码中交换2条命令。

The and dl, 11110000b instruction before the shr dl, 4 instruction is redundant. shr dl, 4指令之前的and dl, 11110000b指令是多余的。 The shift right will by itself throw out the low 4 bits. 右移本身将抛出低4位。

There are a couple of things that I would like to draw your attention to. 我想提醒您一些注意事项。

  • How the nibbles are combined 蚕豆如何结合

    get the 4 right bits of the first number, the 4 left bits of the second number and then merge those 8 bits 得到第一个数字的右4位,第二个数字的左4位,然后合并这8个位

    The logical way to present this combination of bits would be to keep those 4 right bits aka low nibble in the low nibble of the result, and to keep those 4 left bits aka high nibble in the high nibble of the result. 表示这些位组合的逻辑方法是将这4个右位(也称为低半字节)保留在结果的低半字节中,并将这4个左位(即又称为高半字节)保留在结果的高半字节中。 Your code doesn't do it that way and neither do the other answers. 您的代码不会那样做,其他答案也不会。 Perhaps because they want to mimic what you've written. 也许是因为他们想模仿您所写的内容。

    If the 1st number is in AL and the 2nd number is in AH then and ax, 0F00Fh will mask away the unwanted bits and or al, ah will leave the combination in AL 如果第一个数字在AL ,而第二个数字在AHand ax, 0F00Fh将掩盖不需要的位,并且or al, ah将组合保留在AL

  • How 13 is replaced by 0 如何将13替换为0

    0Dh is the ASCII of enter. 0Dh是输入的ASCII。 I need to make sure that enter isn't one of the chars that user had input. 我需要确保enter不是用户输入的字符之一。 If it does 0 should replace it. 如果为0,则应将其替换。

    I think that you could be misinterpreting this "...0 should replace it." 我认为您可能会误解此“ ... 0应该替换它”。
    Probably this is DOS and input was terminated with enter , and so a carriage return (13) was appended to the inputted characters. 可能是DOS,输入用enter终止,因此在输入的字符后附加了回车符(13)。 What your teachers warns about is that the value 13 cannot become part of the result. 您的老师警告的是,值13不能成为结果的一部分。 You replace it with zero in the calculation but not in memory . 在计算中将其替换为零, 但在内存中则不使用

If this is a one-time calculation 如果这是一次计算

Returning a result in AL and keeping SI put. AL返回结果并保留SI

    mov     ax, [si]
    cmp     al, 13
    jne     L1
    xor     ax, ax      ; Falling through shaves off an instruction
L1:
    cmp     ah, 13
    jne     L2
    xor     ah, ah
L2:
    and     ax, 0F00Fh
    or      al, ah
    ret

If this has to be repeated for all characters in the string 如果必须对字符串中的所有字符重复此操作

Always returning a result in AL and having SI point at either the remaining characters or the terminating carriage return. 始终在AL返回结果,并让SI指向其余字符或终止回车符。

Again:
    mov     ax, [si]
    cmp     al, 13      ; If 1st byte is 13, then next byte is just garbage!
    je      CR1         ; ... so no further interpretation needed
    and     al, 0Fh
    inc     si
    cmp     ah, 13      ; If 2nd byte is 13, then result is based on 1st byte
    je      CR2         ; ... and that kind-of zero-replacement
    and     ah, 0F0h
    inc     si
    or      al, ah

    ...

    jmp     Again

CR1:
    xor     al, al
CR2:
    ...

The goal of this code is to get the 4 right bits of the first number,the left bits from the second number from the array (which its address is located in si) and then to merge those 8 bits- and to put the result in a 8 bit register. 该代码的目标是获取数组第一个数字的右4位,数组第二个数字的左位(其地址位于si中),然后合并这8位,并将结果放入8位寄存器。 0dh is the ASCII of enter,I need to make sure that enter isn't one of the chars that user had input,if it does 0 should replace it. 0dh是输入的ASCII,我需要确保输入不是用户输入的字符之一,如果输入为0,则应替换它。

The task doesn't say exactly what the combo should look like, so just bringing them together in any register should suffice. 该任务并未确切说明该组合的外观,因此仅将它们组合到任何寄存器中就足够了。

mov ax, [si] loads AL with the 1st number and AH with the 2nd number. mov ax, [si]AL加载为第一个数字,将AH加载为第二个数字。

The 4 right bits of the 1st number are in the low nibble (bits 3 to 0). 第一个数字的右4位在低半字节(位3到0)中。
The 4 left bits of the 2nd number are in the high nibble (bits 7 to 4). 第二个数字的左4位在高半字节(位7至4)中。

ROR ax, 4 will rotate the low nibble of AX into bits 15-12, and shift the high nibble of AX down into bits 11-8. ROR ax, 4将把AX的低半字节旋转到15-12位,并将AX的高半字节向下移位到11-8位。 Now AH holds the combination of bits. 现在AH拥有位的组合。

 mov ax, [si]
 cmp al, 13
 je  CR
 cmp ah, 13
 je  CR
 ror ax, 4            ; combine the digits in al and ah into al
 ret
CR:
 mov byte [si], 0
 ret

This has but 9 instructions. 这只有9条指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更高效的汇编代码? - More efficient assembly code? 代码在哪里可以更有效地检查输入字符是否为元音? - Where can the code be more efficient for checking if an input character is a vowel? 如何重新构造此代码以提高效率? 8086电梯模拟器 - How to re-structure this code to be more efficient? 8086 Lift Simulator 我如何才能进一步优化此代码? [组装8086字母金字塔] - How I can optimize this code more? [Assembly 8086 letters pyramid] 如何在8051汇编中编写高效的开关()? - How do I write an efficient switch() in 8051 assembly? 如何从第二阶段引导加载程序将更多代码加载到 memory? - How i can load more code to memory from a second-stage bootloader? 以下是 VS2012 的一些汇编代码,我如何在 gcc 上编写它? - Following is some assembly code by VS2012, how can I write it on gcc? 如何编写在现代x64处理器上高效运行的自修改代码? - How can I write self-modifying code that runs efficiently on modern x64 processors? 我如何在 VS Code 中编写和执行 FASM 代码 - How do i write and execute FASM code in VS Code 我可以在什么环境下编写操作系统的二进制代码? - What environment can I use to write binary code of the operating system?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM