在汇编语言中如何显示总和?

huangapple go评论55阅读模式
英文:

How can I display total sum in assembly language?

问题

以下是您的代码的翻译部分:

org 100h

section .data
    roll_num db '210405702'     ; 学号
    roll_len equ $ - roll_num   ; 学号字符串的长度
    sum_msg db '总和为: $'
    output_buffer db 16         ; 用于存储总和的转换字符串的缓冲区
    result_msg db '接收到的数字为: $'

section .text
    global _start

_start:
    mov si, 0
    mov cx, 0
    
sum_loop:
    mov al, byte [roll_num + si] ; 改变 'a1' 为 'al'(8位寄存器)
    sub al, 48                  ; 减去 48 以将ASCII转换为十进制
    add cx, ax                  ; 将 'ax' 改为 'cx' 进行加法
    inc si                      ; 将si增加1
    cmp si, roll_len            ; 将si与roll_len比较
    jl sum_loop                 ; 如果si < roll_len,则跳回sum_loop
    
    mov ax, cx
    
    test al, 1                  ; 改变 'a1' 为 'al'(8位寄存器)
    jz even
    
    mov di, 1
    jmp print_result

even:
    mov di, 0
    
print_result:
    mov ah, 09h
    cmp di, 0
    je even_num
    mov dx, odd_msg             ; 显示奇数消息
    int 21h
    jmp end_program

even_num:
    mov dx, even_msg            ; 显示偶数消息
    int 21h

    ; 调用print_sum以显示总和
    call print_sum
    
end_program:
    mov ah, 4Ch
    xor al, al
    int 21h

print_sum:
    mov ah, 09h
    mov dx, sum_msg             ; 显示总和消息
    int 21h
    
    mov bx, 10                  ; 除以10以将总和转换为十进制字符串
    xor cx, cx                  ; 清除cx寄存器
    mov si, output_buffer + 15  ; 从缓冲区的末尾开始
    
convert_loop:
    xor dx, dx                  ; 清除dx寄存器
    div bx                      ; 用10除ax
    add dl, 48                  ; 将余数转换为ASCII
    dec si                      ; 减少si
    mov [si], dl                ; 将转换后的数字存储在缓冲区中
    inc cx                      ; 增加数字计数
    test ax, ax
    jnz convert_loop            ; 如果ax != 0,则跳回convert_loop
    
    mov dx, si                  ; 显示转换后的总和字符串
    add dx, 1                   ; 增加dx以跳过字符串末尾的'$'
    mov ah, 09h
    int 21h

section .data
    odd_msg db '接收到的数字为奇数', 0Dh, 0Ah, '$'
    even_msg db '接收到的数字为偶数', 0Dh, 0Ah, '$'

请注意,这个翻译是您提供的汇编代码的翻译部分,不包括问题或其他信息。如果您有关于代码的任何问题,请随时提出。

英文:

I'm working on program to calculate the sum in roll number and display output as the sum is even or odd. I have done everything and I'm getting the right output. The only issue is that my programing is not displaying the total sum on screen. It is just display the even or odd after calculation. I also want to display the sum output on screen.

here is my code:

org 100h

section .data
    roll_num db &#39;210405702&#39;     ; Roll Number
    roll_len equ $ - roll_num   ; Length of roll number string
    sum_msg db &#39;The sum is: $&#39;
    output_buffer db 16         ; Buffer to store the converted sum as a string
    result_msg db &#39;The received number is: $&#39;

section .text
    global _start

_start:
    mov si, 0
    mov cx, 0
    
sum_loop:
    mov al, byte [roll_num + si] ; Changed &#39;a1&#39; to &#39;al&#39; (8-bit register)
    sub al, 48                  ; Subtract 48 to convert from ASCII to decimal
    add cx, ax                  ; Changed &#39;ax&#39; to &#39;cx&#39; for addition
    inc si                      ; Increment si by 1
    cmp si, roll_len            ; Compare si with roll_len
    jl sum_loop                 ; Jump back to sum_loop if si &lt; roll_len
    
    mov ax, cx
    
    test al, 1                  ; Changed &#39;a1&#39; to &#39;al&#39; (8-bit register)
    jz even
    
    mov di, 1
    jmp print_result

even:
    mov di, 0
    
print_result:
    mov ah, 09h
    cmp di, 0
    je even_num
    mov dx, odd_msg             ; Display odd message
    int 21h
    jmp end_program

even_num:
    mov dx, even_msg            ; Display even message
    int 21h

    ; Call print_sum to display the total sum
    call print_sum
    
end_program:
    mov ah, 4Ch
    xor al, al
    int 21h

print_sum:
    mov ah, 09h
    mov dx, sum_msg             ; Display the sum message
    int 21h
    
    mov bx, 10                  ; Divide by 10 to convert sum to decimal string
    xor cx, cx                  ; Clear cx register
    mov si, output_buffer + 15  ; Start at the end of the buffer
    
convert_loop:
    xor dx, dx                  ; Clear dx register
    div bx                      ; Divide ax by 10
    add dl, 48                  ; Convert remainder to ASCII
    dec si                      ; Decrement si
    mov [si], dl                ; Store converted digit in buffer
    inc cx                      ; Increment digit count
    test ax, ax
    jnz convert_loop            ; Jump back to convert_loop if ax != 0
    
    mov dx, si                  ; Display the converted sum string
    add dx, 1                   ; Increment dx to skip the &#39;$&#39; at the end of the string
    mov ah, 09h
    int 21h

section .data
    odd_msg db &#39;The received number is odd&#39;, 0Dh, 0Ah, &#39;$&#39;
    even_msg db &#39;The received number is even&#39;, 0Dh, 0Ah, &#39;$&#39;

I tried to write some code. The code is working fine but still it's not showing total sum in output.

答案1

得分: 1

output_buffer db 16
xor cx, cx
mov si, output_buffer + 15
mov  ax, cx
xor  cx, cx
mov  si, output_buffer + 15
mov  byte [si], "$"
mov dx, si
add dx, 1
mov ah, 09h
int 21h
add cx, ax
mov dx, odd_msg
int 21h
jmp end_program
    mov ah, 09h
    int 21h
section .data
英文:

> output_buffer db 16

You don't have a buffer. The above just provides a single byte whereas your program expects a 16-byte buffer. Use output_buffer db 16 dup (0), or output_buffer times 16 db 0 (depending on the assembler).

> xor cx, cx
> mov si, output_buffer + 15

By the time you reach print_sum, the AX register no longer contains the sum, but CX does. And the string needs $-termination. Therefore write it like:

mov  ax, cx
xor  cx, cx
mov  si, output_buffer + 15
mov  byte [si], &quot;$&quot;

> mov dx, si ; Display the converted sum string
> add dx, 1 ; Increment dx to skip the '$' at the end of the string
> mov ah, 09h
> int 21h

It is wrong to add 1 to DX because SI was already pointing at the first digit to display.


> add cx, ax ; Changed 'ax' to 'cx' for addition

Make sure AH is 0 before you do this word-sized addition.

> mov dx, odd_msg ; Display odd message
> int 21h
> jmp end_program

You exit the program before printing the sum in case the sum is odd. Maybe this is intentional? If it is, it is bad because the sum is indeed an odd number (21).

> mov ah, 09h
> int 21h
>
> section .data

Here you have forgotten to exit the program!

    mov ah, 09h
    int 21h
    jmp end_program         &lt;&lt;&lt;&lt; new

section .data

答案2

得分: 0

Here's the translated content:

首先将 output_buffer db 16 更改为 output_buffer db 16 dup(0)

在这个示例中,和为奇数,等于21。
然后,您使用 test al, 1,所以ZF被清除,因为最低有效位是1。

0 0 0 1 0 1 0 1

0 0 0 0 0 0 0 1

0 0 0 0 0 0 0 1 ZF=0

接下来DI = 1,您跳转到print_result,打印odd_msg并跳转到end_program。完全省略了Print_sum。

您应该将 jmp end_program 更改为 jmp print_sum
并且 call print_sumjmp print_sum 相同,因为当您使用call时,指令指针被放入堆栈,而代码中没有ret指令。

在sum_loop之后使用 push ax 以保持sum在convert_loop之前不变,然后在convert_loop之前使用 pop ax

和sum一起打印在屏幕上,并在其后添加“接收到的数字是:”,因为在字符串末尾有$时,打印字符串会停止。因此,在convert_loop之前,您应该打印result_msg,然后计算sum。也许在字符串中加入以$结尾的行。

移动si,output_buffer + 15 ;$在缓冲区的末尾
移动dl,'$'
移动[si],dl
移动si,output_buffer + 14 ;并且我们在$之前放置数字

在.section .data之前添加 jmp end_program

英文:

First change output_buffer db 16 to output_buffer db 16 dup(0)

In this example sum is odd and equal 21.
Then You use test al, 1 so ZF is cleared because least sigificant bit is 1.

0 0 0 1 0 1 0 1

0 0 0 0 0 0 0 1

0 0 0 0 0 0 0 1 ZF=0

Next DI = 1 and You jump to print_result, print odd_msg and jmp to end_program. Print_sum is totally omitted.

You should change jmp end_program to jmp print_sum.
And call print_sum same to jmp print_sum because when You use call, instruction pointer is put on the stack and You don't have ret instruction in the code.

And use push ax to keep sum intact after sum_loop and pop ax before convert_loop.

Sum is printed on the screen + "The received number is:" after it because printing string stops when $ is at the end of the string. So You should print result_msg before convert_loop and then calculate sum. And maybe add before convert_loop lines with $ as last character in string

 mov si, output_buffer + 15     ;$ is last in the buffer
 mov dl,&#39;$&#39;
 mov [si],dl
 mov si, output_buffer + 14    ; and we put digits before $

Add jmp end_program before section .data

:)))

huangapple
  • 本文由 发表于 2023年5月22日 04:46:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定