You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

汇编语言作业咨询:数组求和输出及用户输入存储程序编写

Assembly Language Solutions to Your Homework Problems

Hey there! Let's work through your two assembly problems step by step.


Problem 1: Sum an Array and Print the Result

Your existing code already does a great job calculating the sum of the array W (the total is 9, stored in the AX register). The only missing part is converting that numeric sum to printable ASCII characters and outputting it. Here's how to modify your code to add that functionality:

.model small
.stack 100h
.data
W dw 1,1,2,3,2
msg2 db 0ah,0dh,'$'  ; Newline characters
.code
main:
 mov ax,@data
 mov ds,ax
 xor ax,ax
 lea si,w
 mov cx,5
X: 
 add ax,[si]
 add si,2
 loop X

; --- New code to print the sum ---
 mov cx,0          ; Initialize counter for digits
 mov bx,10         ; Divisor for decimal conversion
convert:
 xor dx,dx         ; Clear DX before division
 div bx            ; AX = AX / 10, DX = remainder (0-9)
 add dl,30h        ; Convert remainder to ASCII
 push dx           ; Push ASCII digit to stack
 inc cx            ; Increment digit count
 cmp ax,0          ; Check if we've processed all digits
 jne convert       ; Repeat if there are more digits

print:
 pop dx            ; Pop digit from stack (high to low)
 mov ah,02h        ; DOS function to print single character
 int 21h           ; Call DOS interrupt
 loop print        ; Print all digits

; Print newline
 lea dx,msg2
 mov ah,09h
 int 21h

; Exit program
 mov ah,4ch
 int 21h
 end main

Key Explanations:

  • Conversion to ASCII: We divide the sum by 10 repeatedly to get each digit as a remainder, then add 30h to convert it to the corresponding ASCII character (e.g., 9 becomes 39h which is '9').
  • Stack Usage: We push each digit to the stack because division gives us digits from least significant to most—popping them reverses the order so we print from most to least significant.
  • DOS Interrupts: int 21h with ah=02h prints a single character, and ah=09h prints a string (used here for the newline).

Problem 2: Read 3 Numbers from User and Store Them

To solve this, we'll read each numeric character from the user, convert it to an integer value, and store it in a memory array. Here's a complete implementation:

.model small
.stack 100h
.data
num_array db 3 dup(0)  ; Array to store 3 numbers
prompt db 'Enter 3 numbers: $'
.code
main:
 mov ax,@data
 mov ds,ax

; Print prompt
 lea dx,prompt
 mov ah,09h
 int 21h

 lea si,num_array   ; Point SI to the start of our array
 mov cx,3           ; Counter for 3 numbers
read_loop:
 mov ah,01h         ; DOS function to read a character (echoes input)
 int 21h            ; AL will hold the ASCII character
 sub al,30h         ; Convert ASCII to integer (e.g., '5' -> 5)
 mov [si],al        ; Store the integer in the array
 inc si             ; Move to next array position
 loop read_loop     ; Repeat for 3 numbers

; Exit program
 mov ah,4ch
 int 21h
 end main

Key Explanations:

  • Reading Input: int 21h with ah=01h reads a single character from the keyboard and echoes it to the screen—this character is stored in AL.
  • ASCII to Integer Conversion: Subtracting 30h (the ASCII value of '0') converts the input character to its integer equivalent (since digits '0'-'9' are consecutive in ASCII).
  • Storage: We use a byte array num_array to hold the three integers, and increment SI each time to move to the next position in the array.

内容的提问来源于stack exchange,提问作者developer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:27:54