Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help w/ 4x4 matrix

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
i'm having trouble writing a proggie. can anyone assist?

i have to write a program that reads 16 numbers (range from 0-999) from the standard input and displays them on the screen as a 4 X 4 matrix. Like this:

0 50 2 200
66 21 54 20
1 3 4 6
100 150 120 99

then, it has to:
1) compute and display the sum of row 1
2) compute and display the sum of col 2
3) count and display the number of values > 100
4) wait for a key to be pressed, and then interchange rows 1 and 2 and redisplay the matrix.
 
here's the code i have so far:

.model small
.stack 100h

.data
numberPrompt db "Enter a number between 0 and 999: ", 0
sumRow1Msg db "The sum of row 1 is: ", 0
sumCol2Msg db "The sum of column 2 is: ", 0
numGtr100Msg db "The number of values > 100 is: ", 0
matrix db 4 dup (?)
db 4 dup (?)
db 4 dup (?)
db 4 dup (?)
count = 16

.code
extrn Writestring:proc, Readint:proc, Writeint:proc, Crlf:proc

main proc
mov ax, @data
mov ds, ax

call GetNums
call DisplayMatrix
call sumRow1
call sumCol2
call Greater100
call Swap1W2
main endp

GetNums proc
mov si, offset matrix
mov cx, count
GetData:
mov dx, offset numberPrompt
call Writestring
call Readint
call Crlf
mov [si], ax
add si, 2
loop GetData
GetNums endp

DisplayMatrix proc
mov si, offset matrix
mov cx, 4
loop:
mov ax, [si]
mov bx, 10
call Writeint
DisplayMatrix endp

;get sum row 1
sumRow1 proc
mov si, offset matrix
mov bx, [si]
mov cx, 3
Sum1:
add si, 2
add bx, [si]
loop Sum1

mov dx,offset sumRow1Msg
call Writestring
mov ax,bx ; get the sum(in BX)
mov bx,10
call Writeint ; display it

sumRow1 endp

;get sum col 2
sumCol2 proc
mov si, offset matrix+1
mov bx, [si]
mov cx, 3
Sum2:
add si, 4
add bx, [si]
loop Sum2

mov dx, offset sumCol2Msg
call Writestring
mov ax,bx ; get the sum(in BX)
mov bx,10
call Writeint ; display it
sumCol2 endp

; count display number vals > 100
;while (cx < count)
;{
; if ([si] < 100)
; count = count + 1
; cx++
; si = si +2
;}
Greater100 proc
mov cx, 1
While:
cmp cx, count
jg Enddo
cmp [si], 100
jl L2
inc bx
L2:
inc cx
add si, 2
jmp While
Enddo:
mov dx, offset numGtr100Msg
call Writestring
mov ax,bx ; get the sum(in BX)
mov bx,10
call Writeint ; display it
Greater100 endp

;swap rows 1 and 2
Swap1W2 proc
mov si, offset matrix
mov di, offset matrix+4
mov cx, 3
rowSwap:
mov bx, [si]
xchg bx, [di]
mov [si], bx
add si, 2
add di, 2
loop rowSwap
Swap1W2 endp

end main
 
Hmm,
Code:
        call Writestring
        call Readint
        call Crlf
        mov [si], ax

How sure are you that the Crlf routine will NOT trash the contents of ax?
Code:
    call sumCol2
    call Greater100
    call Swap1W2
After this.... you're not going to do a DisplayMatrix? Swap1W2 isn't going to display the matrix...


&quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top