English 中文(简体)
我的《集会法》是行不通的。
原标题:my assembly code doesnt run and i dont understand why

The assembly code initializes arrays A and B, calculates the element-wise sum of these arrays, and stores the results in array C. It also provides a procedure, printarray, to print the entire contents of an array and uses this procedure to print the values of arrays A, B, and C to the console. The code assumes a DOS-like environment and requires an x86-compatible system with appropriate assembly tools for execution. yet it didnt give me any result and when i tried debugging it exited the program

.model  small
.stack   100h
.data
A dw 12, 5, 8, -1, 4
B dw -2, 9, 0, 18, 3
C dw 5 dup (?)
N dw 5
.code
mov ax,@data
mov ds,ax
push offset A
push offset B
push offset C
push N
call arraysum
push offset A
push N
call printarray
mov ah, 2
mov dl, 10   
int 21h
push offset B
push N
call printarray
mov ah, 2
mov dl, 10   
int 21h
push offset C
push N
call printarray
mov ah, 2
mov dl, 10   
int 21h
.exit
sum proc near
push bp
mov bp,sp
mov ax,[bp+4]   
add ax,[bp+6]
pop bp
ret 4
sum endp
arraysum proc near
push bp
mov bp, sp
mov cx,[bp+4]
mov di, [bp + 10]
mov si, [bp + 8]
mov bx,[bp+6]
next:
push word ptr [di]
push word ptr [si]
call sum
pop word ptr [di]
pop word ptr [si]
mov [bx], ax
add si, 2  
add di, 2  
add bx, 2
dec cx
jnz next
pop bp
ret 8
arraysum endp

printnum proc near
    push bp
    mov bp, sp
    mov ax, [bp + 4] 
    mov bx, 10
    mov cx, 0
    
    cmp ax, 0 
    jge next
    neg ax
    
next1:   
    mov dx, 0
    div bx
    add dx, 30h
    push dx
    inc cx
    cmp ax, 0
    jne next1
    
    mov ax,[bp+4]
    cmp ax, 0
    jge sof
    push  - 
    inc cx
    
sof:
    cmp cx, 0
    jz ext
    pop dx
    mov ah, 2
    int 21h
    dec cx
    jmp sof
    
ext:
    pop bp
    ret 2   
printnum endp
printarray proc near
    push bp
    mov bp, sp
    mov cx, [bp + 4]  
    mov si, [bp + 6]  
    
print_loop:
    push word ptr [si]  
    call printnum       
    add sp, 2          
    inc si            
    dec cx            
    jnz print_loop     

    pop bp
    ret 4
printarray endp


end
问题回答

I changed/added few things.

  1. OPTION NOKEYWORD:<C>, compiler no longer complains that this is keyword.
  2. Lines push offset A, B or C don t work so I changed this to mov dx, offset X, push dx.
  3. In sum proc, line ret 4 should be ret because after sum proc program pops values and ret 4 changed stack pointer to wrong location.
  4. Order of instructions after call to sum proc, should be pop word ptr [si], pop word ptr [di].
  5. printarray proc, value in cx is destroyed inside printnum proc so I added push cx / pop cx.
  6. In printnum proc I added 2 bytes to offset in lines mov ax, [bp + 6] because of the above cx instructions and procedure ends with ret not ret 2.
  7. Program ends correctly ax = 4c00h, int 21h.
  8. Code prints between values.

Result:

12 5 8 -1 4
-2 9 0 18 3
10 14 8 17 7

Code:

OPTION NOKEYWORD:<C>

.model  small
.stack   100h

.data
    A dw 12, 5, 8, -1, 4        
    B dw -2, 9, 0, 18, 3        
    C dw 5 dup (?)              
    N dw 5                      
    
.code

start:
    mov ax,@data
    mov ds,ax

    mov dx,offset A
    push dx
    mov dx, offset B
    push dx
    mov dx,offset C
    push dx
    push N

    call arraysum

    mov dx, offset A
    push dx
    push N

    call printarray

    mov ah, 2
    mov dl, 10   
    int 21h

    mov dx, offset B
    push dx
    push N

    call printarray

    mov ah, 2
    mov dl, 10   
    int 21h

    mov dx, offset C
    push dx
    push N

    call printarray

    mov ah, 2
    mov dl, 10   
    int 21h

exit:
    mov ax,4c00h
int 21h

sum proc near
    push bp
    mov bp,sp
    mov ax,[bp+4]   
    add ax,[bp+6]
    pop bp
    ret                 
sum endp

arraysum proc near
    push bp
    mov bp, sp
    mov cx,[bp+4]           ;; num rep
    mov di, [bp + 10]       ;; off array A
    mov si, [bp + 8]        ;; off array B
    mov bx, [bp+6]          ;; off array C 

next:
    push word ptr [di]      ;; push elements A[i] and B[i], i = 0 .. 4
    push word ptr [si]
    
    call sum
    
    pop word ptr [si]
    pop word ptr [di]       
                          
    mov [bx], ax
    add si, 2  
    add di, 2  
    add bx, 2
    dec cx
    jnz next
    pop bp
    ret 8           
arraysum endp

printnum proc near
    push bp
    mov bp, sp
    mov ax, [bp + 6]            
    mov bx, 10
     
    mov cx, 0                   
    
    cmp ax, 0 
    jge next1
    neg ax
    
next1:   
    mov dx, 0
    div bx
    add dx, 30h
    push dx
    inc cx
    cmp ax, 0
    jne next1
    
    mov ax,[bp + 6]
    cmp ax, 0
    jge sof
     xor ax,ax
     mov al, - 
    push ax
    inc cx
    
sof:
    cmp cx, 0
    jz ext
    pop dx
    mov ah, 2
    int 21h
    dec cx
    jmp sof
    
ext:
    pop bp
    
    ret         ;
printnum endp

printarray proc near
    push bp
    mov bp, sp
    mov cx, [bp + 4] 
    mov si, [bp + 6] 
    
print_loop:
    push word ptr [si]  
       push cx 
    call printnum
pop cx  
    add sp, 2          
    add si,2               
    dec cx     

    mov ah, 2
    mov dl,       
    int 21h
    
    jnz print_loop     

    pop bp
    ret 4
printarray endp

end start

这里的问题是法典中的所有问题:

  1. http://www.un.org。 在守则中,你利用监督事务司打断了<条码>第21条h,而AH则把方案撤回到4Ch。 然而,在打断前,你先在ALA中设定出国代码。 在打断这一中断之前,你应将退出法装进AL。 例如,可在<编码>前添加<代码>mov al, 0>,将退出代码定为0。

  2. http://www.un.org。 该守则似乎通过添加一种特性来处理<条码>南程序中的负面数字。 虽然这一工程是为了显示目的,但它并没有修改实际数量。 如果您打算以负面数字开展工作,你就不得不考虑其对计算的影响。

  3. No Error Handling: The code doesn t have error-handling mechanisms. If you encounter errors during execution (e.g., division by zero), the program may behave unexpectedly or crash. You should include error-checking and handling code to make the program more robust.

The code to make the assembly code run correctly.

法典:

.model small
.stack 100h
.data
A dw 12, 5, 8, -1, 4
B dw -2, 9, 0, 18, 3
C dw 5 dup (?)
N dw 5

.code
mov ax, @data
mov ds, ax

push offset A
push offset B
push offset C
push N
call arraysum

push offset A
push N
call printarray

mov ah, 4Ch   ; Exit program with code in AL
int 21h

sum proc near
    push bp
    mov bp, sp
    mov ax, [bp + 4]
    add ax, [bp + 6]
    pop bp
    ret 4
sum endp

arraysum proc near
    push bp
    mov bp, sp
    mov cx, [bp + 4]
    mov di, [bp + 10]
    mov si, [bp + 8]
    mov bx, [bp + 6]
next:
    push word ptr [di]
    push word ptr [si]
    call sum
    pop word ptr [di]
    pop word ptr [si]
    mov [bx], ax
    add si, 2
    add di, 2
    add bx, 2
    dec cx
    jnz next
    pop bp
    ret 8
arraysum endp

printnum proc near
    push bp
    mov bp, sp
    mov ax, [bp + 4]
    mov bx, 10
    mov cx, 0
    cmp ax, 0
    jge next
    neg ax
next1:
    mov dx, 0
    div bx
    add dl,  0    ; Convert remainder to ASCII
    push dx
    inc cx
    cmp ax, 0
    jne next1
    mov ax, [bp + 4]
    cmp ax, 0
    jge sof
    push  -       ; Negative sign
    inc cx
sof:
    cmp cx, 0
    jz ext
    pop dx
    mov ah, 2
    int 21h
    dec cx
    jmp sof
ext:
    pop bp
    ret 2
printnum endp

printarray proc near
    push bp
    mov bp, sp
    mov cx, [bp + 4]
    mov si, [bp + 6]
print_loop:
    push word ptr [si]
    call printnum
    add sp, 2
    add si, 2
    dec cx
    jnz print_loop
    pop bp
    ret 4
printarray endp

end

最后一个问题 WHYYYY ARE YOU USING Assembly! 他只是玩弄





相关问题
How can I do a CPU cache flush in x86 Windows?

I am interested in forcing a CPU cache flush in Windows (for benchmarking reasons, I want to emulate starting with no data in CPU cache), preferably a basic C implementation or Win32 call. Is there a ...

What are the names of the new X86_64 processors registers?

Where can I find the names of the new registers for assembly on this architecture? I am referring to registers in X86 like EAX, ESP, EBX, etc. But I d like them in 64bit. I don t think they are the ...

How do I disassemble raw 16-bit x86 machine code?

I d like to disassemble the MBR (first 512 bytes) of a bootable x86 disk that I have. I have copied the MBR to a file using dd if=/dev/my-device of=mbr bs=512 count=1 Any suggestions for a Linux ...

32bit application access to 64bit registry

I have an OS Shell written in 32bit that is replacing the Explorer.exe of a Vista machine. I run a utility which is also written in 32bit, which allows to switch between the Explorer shell and My ...

Left Shift Overflow on 68k/x86?

I heard that the Motorola 68000 and Intel x86 architectures handle overflow from left shifting differently. Specifically the 68k LSL vs. the Intel SAL/SHL assembly instructions. Does anyone know the ...

Translate a FOR to assembler

I need to translate what is commented within the method, to assembler. I have a roughly idea, but can t. Anyone can help me please? Is for an Intel x32 architecture: int secuencia ( int n, ...

热门标签