【彙編語言】記錄一組數中負數的個數,8086與MIPS彙編程序

題目及解答

統計由DATA開始的字節數據串中負元素的個數,數據個數在COUNT單元,統計結果存入RLT單元。

8086彙編:

; 統計數字中負數的個數【循環中加了個if else】
assume ds:datasg
datasg segment

	data	db 1,-2,-3,-1,-4,0,-2 
	count	dw 7	; 數組有8個數字
	plr		dw ?	; 數字爲負數的個數
	
datasg ends

assume cs:code
code segment
start:
	mov ax,datasg
	mov ds,ax
	
	mov dl,0 ; 暫存數據
	mov ax,0 ; 記錄負數個數
	mov bx,offset data ; 遍歷數組
	mov cx,count
	s:
		; 判斷是否 <0
		mov dl,[bx]
		cmp dl,0
		jl  ok ; < 0
		jmp no ; >= 0
		
		ok:
			inc ax
			
		no:
			inc bx
	loop s
	
	mov plr,ax
	
	mov ax,4c00h
	int 21h
code ends
end start

MIPS彙編

.data

	numArray:	.byte	-2,-3,-4,-6,8,0,-2
	arrayCount:	.word	7		# 數組大小
	plr:		.space	32		# 負數的個數
	
.text
.globl main
main:
	
	la	$t2,numArray	# 數組首地址
	li	$t3,0			# 存放負數的個數
	lw	$t0,arrayCount	# 循環次數
	li	$t1,0			# 相當於for循環的int i = 0
	s:
		lb  $s0,($t2)
		blt $s0,0,ok
		j no
		
		ok:
		addi $t3,$t3,1
		no:
		addi $t2,$t2,1
		
		addi $t1,$t1,1		# i = i + 1
	blt	 $t1,$t0,s
	
	sw	$t3,plr	# 寫回內存
	
exit:
	li $v0,10
	syscall
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章