Assembly Language
प्रवाह नियंत्रण
खोज…
परिचय
सामान्य ज्ञान IF-THEN-ELSE m68k विधानसभा में
; IF d0 == 10 GO TO ten, ELSE GO TO other
CMP #10,d0 ; compare register contents to immediate value 10
; instruction affects the zero flag
BEQ ten ; branch if zero flag set
other:
; do whatever needs to be done for d0 != 10
BRA afterother ; unconditionally jump across IF case
ten:
; do whatever needs to be done for d0 == 10
afterother:
; continue normal common program flow
कौन से निर्देश प्रभावित कर रहे हैं कि कौन से झंडे, और कौन से सशर्त शाखाएँ (जो कि झंडे के विशिष्ट संयोजनों पर भी आधारित हो सकती हैं) उपलब्ध हैं, आपके चुने हुए सीपीयू पर बहुत निर्भर करता है और इसे मैनुअल में देखा जाना चाहिए।
Z80 असेंबली में अगला ...
Z80 में लूप काउंट्स को लागू करने के लिए एक विशिष्ट निर्देश है: DJNZ
"डीक्रिमेंट बी रजिस्टर और जंप नॉट जीरो" के लिए खड़ा है। तो, बी इस प्रोसेसर पर छोरों को लागू करने के लिए पसंद का रजिस्टर है। के लिए ... अगला "पीछे की ओर" लागू करने की आवश्यकता है, क्योंकि रजिस्टर शून्य तक गिना जाता है। अन्य सीपीयू (8086 की तरह, यह सीपीयू लूप काउंटर के रूप में सीएक्स रजिस्टर का उपयोग करता है) समान विशिष्ट लूप काउंटर रजिस्टर और निर्देश हो सकता है, कुछ अन्य सीपीयू मनमाने ढंग से रजिस्टरों के साथ लूप कमांड की अनुमति देते हैं (m68k में एक DBRA निर्देश है जो किसी भी डेटा रजिस्टर के साथ काम करता है)।
; Trivial multiplication (by repeated adding, ignores zero in factors, so
; not recommended for general use)
;
; inputs: A = Factor 1
; B = Factor 2
;
; output: A = Factor 1 * Factor 2
;
; Pseudo code
; C = A : A = 0 : FOR B = Factor 2 DOWNTO 0 : A = A + C : NEXT B
mul:
LD C,A ; Save Factor 1 in C register
XOR A ; Clear accumulator
mLoop:
ADD A,C ; Add Factor 1 to accumulator
DJNZ mLoop ; Do this Factor 2 times
RET ; return to caller
इफ-सिंटैक्स असेंबली में अगर-स्टेटमेंट
section .data
msg_eq db 'Equal', 10
len_eq equ $ - msg_eq
msg_le db 'Less than', 10
len_le equ $ - msg_le
msg_gr db 'Greater than', 10
len_gr equ $ - msg_gr ; Length of msg_gr
section .text
global _main ; Make the _main label global for linker
_main:
cmp 4, 5 ; Compare 4 and 5
je _equal ; je = jump if equal
jl _less ; jl = jump if less
jg _greater ; jg = jump if greater
exit:
ret ; Return
_equal:
; Whatever code here
mov rax, 0x2000004 ; sys_write, 4 for linux
mov rdi, 1 ; STDOUT
mov rsi, msg_eq
mov rdi, len_eq
syscall
jmp exit ; Exit
_less:
; Whatever code here
mov rax, 0x2000004
mov rdi, 1
mov rsi, msg_le
mov rdi, len_le
syscall
jmp exit
_greater:
; Whatever code here
mov rax, 0x2000004
mov rdi, 1
mov rsi, msg_gr
mov rdi, len_gr
syscall
jmp exit
इंटेल सिंटैक्स असेंबली में स्थिति के अनुसार लूप सही है
section .data
msg db 'Hello, world!', 0xA
len equ $ - msg
section .text
global _main
_main:
mov rax, 0 ; This will be the current number
mov rcx, 10 ; This will be the last number
_loop:
cmp rax, rcx
jl .loopbody ; Jump to .loopbody if rax < rcx
jge _exit ; Jump to _exit if rax ≥ rcx
.loopbody:
push rax ; Store the rax value for later use
mov rax, 0x2000004 ; 4 for Linux
mov rdi, 1 ; STDOUT
mov rsi, msg
mov rdx, len
syscall
pop rax ; Take it back to rax
inc rax ; Add 1 to rax. This is required since the loop must have an ending.
jmp _loop ; Back to loop
_exit:
ret ; Return
यह .loopbody
को rax < rcx
रूप में लंबे समय तक निष्पादित करेगा।