如何使用汇编语言中的循环指令在控制台中打印10至19?

问题描述

我可以使用循环打印0到9,但不能打印10到19。我该怎么做?

我使用此代码打印0到9:

.model small
.stack 100h
.code
main proc
    
    mov cx,10
    mov dl,'0'
    
    lbl1:
        mov ah,2
        int 21h
        inc dl
        loop lbl1

    main endp
end main

解决方法

如果您只需要打印10到19之间的数字,那么杰斯特的建议将“ 1”字符放在前面是极好的选择。必须小心,因为输出“ 1”字符会破坏DL寄存器中的循环变量。

    mov  cx,10
    mov  dl,'0'
lbl1:

*   push dx        ; Preserve our loop variable
*   mov  dl,"1"
*   mov  ah,02h   ; DOS.PrintCharacter
*   int  21h
*   pop  dx        ; Restore our loop variable

    mov  ah,02h   ; DOS.PrintCharacter
    int  21h
    inc  dl
    loop lbl1

如果您将数字打印在彼此的上方,将会更好看。我们在代码中添加了回车符和换行符对:

    mov  cx,'0'
lbl1:
    push dx        ; Preserve our loop variable
    mov  dl,"1"
    mov  ah,02h   ; DOS.PrintCharacter
    int  21h
    pop  dx        ; Restore our loop variable
    mov  ah,02h   ; DOS.PrintCharacter
    int  21h

*   push dx        ; Preserve our loop variable
*   mov  dl,13    ; Carriage return
*   mov  ah,02h   ; DOS.PrintCharacter
*   int  21h
*   mov  dl,10    ; Linefeed
*   mov  ah,02h   ; DOS.PrintCharacter
*   int  21h
*   pop  dx        ; Restore our loop variable

    inc  dl
    loop lbl1
,

您需要实现一个完整的整数到字符串的转换算法,例如:https://codereview.stackexchange.com/a/142910/178169

当前,您只是在打印所有数字ASCII字符,其中仅存在 float duration = 1.0f; float startTime; float time; string lastTrigger = null; float TransitionTwoValues(float minVal,float maxVal){ float vTemp = maxVal; maxVal = minVal; minVal = vTemp; return Mathf.SmoothStep(minVal,maxVal,time); } void ResetStartTimeOnce(string currentTriggerName,string lastTriggerName){ if(currentTriggerName != lastTrigger){ lastTrigger = lastTriggerName; startTime = Time.time; } } void CalcTransition(){ time = (Time.time - startTime) / duration; } void Focus(){ duration = 0.1f; ResetStartTimeOnce("FocusOn",lastTrigger); CalcTransition(); _Vignette.intensity.value = TransitionTwoValues( vignetteMaxVal,vignetteMinVal); _MotionBlur.intensity.value = TransitionTwoValues(motionBlurMaxVal,motionBlurMinVal); _ColorAdjustment.saturation.value = TransitionTwoValues(colorSaturationMaxVal,colorSaturationMinVal); } void FocusRelease(){ duration = 0.5f; ResetStartTimeOnce("FocusOff",lastTrigger); CalcTransition(); _Vignette.intensity.value = TransitionTwoValues( vignetteMinVal,vignetteMaxVal); _MotionBlur.intensity.value = TransitionTwoValues(motionBlurMinVal,motionBlurMaxVal); _ColorAdjustment.saturation.value = TransitionTwoValues(colorSaturationMinVal,colorSaturationMaxVal); } void Update(){ if(Input.GetMouseButtonDown(1)){ Focus(); Debug.Log(time); //** TIME ALWAYS 0 WHY? } if(Input.GetMouseButtonUp(1)){ FocusRelease(); } } } -0。相反,您需要将它们组合起来以形成一个字符串,该字符串与要打印的数字相对应。