Assembly 如何使用emu8086检查平衡圆括号 < P>检查是否平衡,我用C++语言编码,但是如何将这个代码转换成汇编EMU8086. #include <iostream> //main header file #include <stack> using namespace std; void balance_parentheses(); int main() { int t; cout << "Enter number of test cases:"; cin >> t; for (int i = 0; i < t; i++) { //calling of function for checking of brackets balance_parentheses(); } return 0; } void balance_parentheses() { stack<char> a; string s; cout << "Enter string may or may not containing parentheses:"; cin >> s; int flag = 0; //flag is an arbitrary variable for (int i = 0; i < s.length(); i++) //for length of the string calculated by number of letters { if (s[i] == '{' || s[i] == '[' || s[i] == '(') { //push function to enter terms in a stack a.push(s[i]); flag = 1; } if (!a.empty()) { if (s[i] == '}') { if (a.top() == '{') // top of the stack { a.pop(); //pop function to delete terms from the top of array continue; } else break; } if (s[i] == ']') { if (a.top() == '[') { a.pop(); continue; } else break; } if (s[i] == ')') { if (a.top() == '(') { a.pop(); continue; } else break; } } else { break; } } if ((a.empty()) && (flag == 1)) cout << "Balaned" << endl; else cout << "Not Balanced" << endl; } #包含//主头文件 #包括 使用名称空间std; 空平衡括号(); int main() { int t; cout>t; for(int i=0;is; int flag=0;//flag是任意变量 对于(int i=0;i代码>通常使用C++ > >代码>定义一个常量。C++程序不使用静态It1616t A1=['因此,您也没有理由在asm中使用静态存储。如果您只编写jne@internal\u if3,而不是使用带有相同标志的je@internal\u if2\u perform跳过jne,那么您也可以降低分支条件的噪音。因此,即使不会执行,也只能到达jne如果它将被采用,并且可能是一个JMP(但正如我所说的,最好删除JE,只保留JNE,所以它要么被采用,要么失败。)在条件相反的情况下连续有两个分支是没有意义的(除非它们之间有分支目标,如果您可以在设置了标志的情况下从其他地方跳入…)检查堆栈数据结构是否为空,无需对绝对堆栈地址进行硬编码,也无需在循环中使用mov bp,sp。例如,将bp设置为正常帧指针,然后进行比较。此外,在按下打开的paren后,您还可以跳过检查,以查看它可能是哪种关闭的parene、 您的mov cx,8似乎是多余的。您从不显式使用cx或CL,也不使用loop指令。相反,您会计算i(出于某种原因在内存中)8.总的来说,这似乎是最好发布到我的评论可以作为答案发布的地方,而不是作为其他人效仿的例子。这太混乱了,我不想推荐它。(当你还在学习的时候,这很正常;几个月或几年后,如果你回头看,你会发现同样的问题。)我决定不投反对票,但我更倾向于这个方向,而不是投赞成票。(好的评论和缩进,使我至少可以通过一些工作来遵循逻辑。)
首先,Assembly 如何使用emu8086检查平衡圆括号 < P>检查是否平衡,我用C++语言编码,但是如何将这个代码转换成汇编EMU8086. #include <iostream> //main header file #include <stack> using namespace std; void balance_parentheses(); int main() { int t; cout << "Enter number of test cases:"; cin >> t; for (int i = 0; i < t; i++) { //calling of function for checking of brackets balance_parentheses(); } return 0; } void balance_parentheses() { stack<char> a; string s; cout << "Enter string may or may not containing parentheses:"; cin >> s; int flag = 0; //flag is an arbitrary variable for (int i = 0; i < s.length(); i++) //for length of the string calculated by number of letters { if (s[i] == '{' || s[i] == '[' || s[i] == '(') { //push function to enter terms in a stack a.push(s[i]); flag = 1; } if (!a.empty()) { if (s[i] == '}') { if (a.top() == '{') // top of the stack { a.pop(); //pop function to delete terms from the top of array continue; } else break; } if (s[i] == ']') { if (a.top() == '[') { a.pop(); continue; } else break; } if (s[i] == ')') { if (a.top() == '(') { a.pop(); continue; } else break; } } else { break; } } if ((a.empty()) && (flag == 1)) cout << "Balaned" << endl; else cout << "Not Balanced" << endl; } #包含//主头文件 #包括 使用名称空间std; 空平衡括号(); int main() { int t; cout>t; for(int i=0;is; int flag=0;//flag是任意变量 对于(int i=0;i代码>通常使用C++ > >代码>定义一个常量。C++程序不使用静态It1616t A1=['因此,您也没有理由在asm中使用静态存储。如果您只编写jne@internal\u if3,而不是使用带有相同标志的je@internal\u if2\u perform跳过jne,那么您也可以降低分支条件的噪音。因此,即使不会执行,也只能到达jne如果它将被采用,并且可能是一个JMP(但正如我所说的,最好删除JE,只保留JNE,所以它要么被采用,要么失败。)在条件相反的情况下连续有两个分支是没有意义的(除非它们之间有分支目标,如果您可以在设置了标志的情况下从其他地方跳入…)检查堆栈数据结构是否为空,无需对绝对堆栈地址进行硬编码,也无需在循环中使用mov bp,sp。例如,将bp设置为正常帧指针,然后进行比较。此外,在按下打开的paren后,您还可以跳过检查,以查看它可能是哪种关闭的parene、 您的mov cx,8似乎是多余的。您从不显式使用cx或CL,也不使用loop指令。相反,您会计算i(出于某种原因在内存中)8.总的来说,这似乎是最好发布到我的评论可以作为答案发布的地方,而不是作为其他人效仿的例子。这太混乱了,我不想推荐它。(当你还在学习的时候,这很正常;几个月或几年后,如果你回头看,你会发现同样的问题。)我决定不投反对票,但我更倾向于这个方向,而不是投赞成票。(好的评论和缩进,使我至少可以通过一些工作来遵循逻辑。),assembly,x86,emu8086,Assembly,X86,Emu8086,首先,@表示标签的开始,$表示过程的开始。如果汇编代码中存在多个标签和过程,则可以轻松区分标签和过程。这只是我自己的编码方式。您可以忽略此样式 .model small ; declaring this code will be consists of one data segment and one code segment .stack 100h ; stack is initializeed to offset address at
@
表示标签的开始,$
表示过程的开始。如果汇编代码中存在多个标签和过程,则可以轻松区分标签和过程。这只是我自己的编码方式。您可以忽略此样式
.model small ; declaring this code will be consists of one data segment and one code segment
.stack 100h ; stack is initializeed to offset address at 100h
.data ; Data segment
n_line db 0ah,0dh,"$" ; for new line
msg1 db 10,13,"Balanced$"
msg2 db 10,13,"Not Balanced$"
a1 dw 5bh ; '['
a2 dw 5dh ; ']'
b1 dw 7bh ; '{'
b2 dw 7dh ; '}'
c1 dw 28h ; '('
c2 dw 29h ; ')'
flag db 0
i db ?
.code ; Code segment
main proc
mov ax,@data ; copying starting address of data segment into ax register
mov ds,ax ; by copying ax into ds we are initializing data segment
mov cx,8 ; length is 8 fixed here
mov i,0d ; https://stackoverflow.com/questions/45904075/displaying-numbers-with-dos
xor ax,ax ; clearing ax
xor bx,bx ; clearing bx
xor dx,dx ; clearing dx
@input:
cmp i,8d
jge @input_end
mov ah,1 ; taking input
int 21h
mov ah,0 ; clearing previously stored 1 in ah
mov bp,sp ; checking stack is empty or not using bp. it can be done sp in this code
@if1: ; in this @if1 level: checking if current input is opening bracket or not
cmp ax,a1
je @push_it
cmp ax,b1
je @push_it
cmp ax,c1
je @push_it
jmp @if2
@push_it: ; if current input is opening bracket then push it
push ax
mov flag,1 ; and alter the default flag value
@if2:
cmp bp,100h ; checking if stack is empty or not
je @if2_end ; if not empty then
@inner_if1: ; in this @inner_if1 level, checking if top of the stack is co-responding closing bracket of '{' or not
cmp ax,b2 ; top==}
je @inner_if1_perform
jne @inner_if2
@inner_if1_perform:
pop bx ; top is popped and storing it to bx
cmp bx,b1
jne @inner_push1
je @inner_if2
@inner_push1:
push bx ; if not matched, then that popped value should be pushed
@inner_if2: ; in this @inner_if2 level, checking if top of the stack is co-responding closing bracket of '[' or not
cmp ax,a2 ; top==]
je @inner_if2_perform
jne @inner_if3
@inner_if2_perform:
pop bx ; top is popped and storing it to bx
cmp bx,a1
jne @inner_push2
je @inner_if3
@inner_push2:
push bx ; if not matched, then that popped value should be pushed
@inner_if3: ; in this @inner_if3 level, checking if top of the stack is co-responding closing bracket of '(' or not
cmp ax,c2 ; top== )
je @inner_if3_perform
jne @inner_if3_end
@inner_if3_perform:
pop bx ; top is popped and storing it to bx
cmp bx,c1
jne @inner_push3
je @inner_if3_end
@inner_push3:
push bx ; if not matched, then that popped value should be pushed
@inner_if3_end:
@if2_end:
inc i
jmp @input
@input_end: ; if ( (flag == 1) && (stack.empty()) )
cmp flag,1
je @next_level:
jne @print_msg2
@next_level:
cmp sp,100h ; cheking the stack pointer is returned to initial address or not means empty or not
je @print_msg1
jne @print_msg2
@print_msg1:
lea dx,msg1
mov ah,9
int 21h
@stop:
mov ah,4ch ; terminate the code
int 21h
main endp ; ending of main procedure
@print_msg2:
lea dx,msg2
mov ah,9
int 21h
jmp @stop
end main ; ending of code segment
; Input:
; [[]({})] ------ Balanced
; {({}[])} ------ Balanced
; []({)}[] ------ Not Balanced
; }()[(]){ ------ Not Balanced
<代码> A1DW 5BH;''/'>代码>通常使用C++ > >代码>定义一个常量。C++程序不使用<代码>静态It1616t A1=['因此,您也没有理由在asm中使用静态存储。如果您只编写
jne@internal\u if3
,而不是使用带有相同标志的je@internal\u if2\u perform
跳过jne
,那么您也可以降低分支条件的噪音。因此,即使不会执行,也只能到达jne如果它将被采用,并且可能是一个JMP(但正如我所说的,最好删除JE,只保留JNE,所以它要么被采用,要么失败。)在条件相反的情况下连续有两个分支是没有意义的(除非它们之间有分支目标,如果您可以在设置了标志的情况下从其他地方跳入…)检查堆栈数据结构是否为空,无需对绝对堆栈地址进行硬编码,也无需在循环中使用mov bp,sp
。例如,将bp设置为正常帧指针,然后进行比较。此外,在按下打开的paren后,您还可以跳过检查,以查看它可能是哪种关闭的parene、 您的mov cx,8
似乎是多余的。您从不显式使用cx或CL,也不使用loop
指令。相反,您会计算i
(出于某种原因在内存中)8.总的来说,这似乎是最好发布到我的评论可以作为答案发布的地方,而不是作为其他人效仿的例子。这太混乱了,我不想推荐它。(当你还在学习的时候,这很正常;几个月或几年后,如果你回头看,你会发现同样的问题。)我决定不投反对票,但我更倾向于这个方向,而不是投赞成票。(好的评论和缩进,使我至少可以通过一些工作来遵循逻辑。)