8086汇编实现杨辉三角(Pascal's triangle)程序错误求助
8086汇编杨辉三角程序调试问题
我尝试编写一个计算杨辉三角(Pascal's triangle)的8086汇编程序,但运行结果不正确,以下是我的代码:
org 100h ; Set an array with size equal to 100 array db 1, 99 dup(0) _start: mov si, 1 ;repreasent if we add already 100 values and we should stop the loop mov di, 1 ;di set to be the row mov bp, 0; bp set to be the index mov [array + di * 10 ], 1 mov [array + di * 10 + 1], 1 inc si inc si inc di ;we start from row number 2 ;lets move to a new row jmp move_to_new_row ;after the move to the enew row is finish we go the full array and we want to print it out mov ax, 0 jmp move_over_array ;this lable make sure to load values to array add_values_to_row: mov al, [array + <(di - 1) * 10> + bp] mov dl, [array + <(di -1) * 10 > + bp -1] add al, dl ; add the arr[line - 1][i] + arr[line- 1][i-1] mov [array + bp], al; set the index to the sum of indedxes in the privios line inc si ; adding to the numbers of adding values 1 ;update the values inc bp;adding 1 to the index move_to_new_row: mov [array + di], 1 ;set the first index of the new row to be 1 mov bp, 1 ; we starting full the row from index = 1 mov cx, 10 ; move the loop 10 times loop add_values_to_row ;full the new row ;now we full a row good lack lets go to the new row inc di; adding 1 to the row cmp si, 100 ;checking if we adding to the array all the 10 lines JB move_to_new_row ; add another line for full print_new_line: mov bl , 0 PRINTN " " print_using_ax: mov bx, ax ; Move the value from al to bx mov al, [array + bx] ; Use bx as an index call print_num inc ax ; Increment the index (assuming 16-bit elements, adjust accordingly) inc bl cmp bl, 10 JE print_new_line move_over_array: mov bl, 0 ;move over the array and print the value using ax register loop print_using_ax int 16h ret ; Include magshimim library used for print and so on include magshimim.inc
代码中的核心问题
1. 内存寻址语法错误
8086汇编不支持[array + <(di - 1) * 10> + bp]这种直接在寻址中做复杂算术运算的写法,必须先把偏移量计算到寄存器中再使用。
2. 数组定位错误
- 行首元素赋值错误:
mov [array + di], 1未考虑每行10个字节的偏移,应改为mov [array + di * 10], 1,指向当前行的起始位置。 - 当前行元素赋值错误:
mov [array + bp], al直接写到数组起始偏移的bp位置,未定位到当前行,应改为mov [array + di * 10 + bp], al。
3. 循环逻辑错误
move_to_new_row中mov cx, 10固定循环10次不合理,杨辉三角第n行(从0开始计数)有n+1个元素,中间需要填充的元素数是n-1个,需根据当前行号动态设置循环次数。move_over_array中使用loop print_using_ax但未初始化cx,loop指令依赖cx的初始值控制循环次数,会导致打印逻辑混乱。
4. 终止条件错误
用cmp si, 100判断是否完成所有行不合理,10行杨辉三角总元素数为55,而非100,应直接按行数判断(比如生成10行后停止)。
修正后的代码
org 100h ; 数组大小100,每行10个元素,最多存储10行杨辉三角 array db 1, 99 dup(0) _start: mov di, 1 ; 当前行号(第0行已初始化array[0]=1) mov si, 1 ; 已填充元素计数,初始第0行有1个元素 ; 初始化第1行(偏移10和11的位置) mov [array + di * 10], 1 mov [array + di * 10 + 1], 1 add si, 2 inc di ; 切换到第2行 move_to_new_row: ; 计算当前行起始偏移:di * 10 mov ax, di mov cx, 10 mul cx mov bx, ax ; 设置当前行首元素为1 mov [array + bx], 1 inc si mov bp, 1 ; 当前行元素索引(从1开始) mov cx, di sub cx, 1 ; 循环次数:当前行需要填充的中间元素数 add_values_to_row: ; 计算上一行起始偏移:(di-1)*10 mov ax, di sub ax, 1 mov dx, 10 mul dx mov si_prev, ax ; 取上一行的两个相邻元素相加 mov al, [array + si_prev + bp] mov dl, [array + si_prev + bp - 1] add al, dl ; 赋值到当前行对应位置 mov [array + bx + bp], al inc si inc bp loop add_values_to_row ; 设置当前行尾元素为1 mov [array + bx + di], 1 inc si inc di cmp di, 10 ; 生成10行后停止 JB move_to_new_row ; 按行打印杨辉三角 print_start: mov di, 0 ; 从第0行开始打印 print_row: ; 计算当前行起始偏移 mov ax, di mov cx, 10 mul cx mov bx, ax mov cx, di inc cx ; 当前行元素个数:di+1 print_element: mov al, [array + bx] call print_num PRINT " " inc bx loop print_element PRINTN "" ; 换行 inc di cmp di, 10 JB print_row int 16h ; 等待按键输入 ret si_prev dw 0 ; 临时存储上一行起始偏移 ; 引入magshimim库用于打印功能 include magshimim.inc
内容的提问来源于stack exchange,提问作者Ravid atia
相关产品推荐
相关产品推荐

