Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Praise should be given to the Forum managers or the Tipmasters - they are what make it work - give them extra recognition!!! They are timely (prompt - unlike ACTUAL support sites) and on the ball!!!..."

Geography

Where in the world do Tek-Tips members come from?
HotCode (Programmer)
29 Sep 11 10:54
Hello, I got this simple program worked up and I just dont think it is doing what is is suppose to do. Now admittedly I am just starting in assembly language and I am still a bit lost.
This is in MASM and I want to sum every number in the array that is less than 50 (sample varable) I think I got it working but then its giving a rather large number, but that might be the index, I might be adding up the index by accident I think I am using the number in the array but not quite sure. Well thanks for any help here is the code

heads up
call WriteInt
call    Crlf
call WriteSting
are from a lib from this guys book
Irvine

CODE


TITLE MASM AM5                        (AM5.asm)

; Description:
;
; Revision date:

INCLUDE Irvine32.inc
.data
myMessage BYTE "Assignment 5",0dh,0ah,0
myMess2    BYTE "The Sum is: " , 0

array DWORD 10, 60, 20, 33, 72, 89, 45, 65, 72, 18
sample DWORD 50
ArraySize = ($ - Array) / TYPE array
sum DWORD 0


.code
main PROC
    call Clrscr  
    
    mov     edx,offset myMessage
    call WriteString
    
    mov ecx,0

    .WHILE ecx < ArraySize            ;while( index < ArraySize)
        mov eax,array[ecx]            ;put whats in the array into a register for easy typeing
            
            .IF eax <= sample        ;is what in the eax/array <= sample
                add sum,eax            ;if so then add it to sum
            .ENDIF    
        inc ecx                        ;index++
    .ENDW

    mov     edx,offset myMess2
    call WriteString                ;display sum message
    mov edi, sum                    ;display sum
    call WriteInt
    call    Crlf
    
    exit
main ENDP

END main
Helpful Member!  Prattaratt (TechnicalUser)
29 Sep 11 16:31
1.  You are incrementing the array index by one BYTE, but the array is in DWORDS, which are 4 BYTES. So the array you are iterating through is:

10, 00, 00 00, 60, 00, 00, 00, 20, 00, 00;
 
2.  Your array size is going to be off because you have an intervening DWORD from then end of array and where you calculate the size of the array.  So instead of arraysize = sizeof array, arraysize = sizeof array + 1.  (Not that it will make any difference in you calculation results, but nipping bad habits now will prevent bugs down the road.)

CODE

.data

myMessage BYTE "Assignment 5",0dh,0ah,0my
Mess2     BYTE "The Sum is: " , 0
array     DWORD 10, 60, 20, 33, 72, 89, 45, 65, 72, 18
ArraySize = ($ - Array) / TYPE array
sample    DWORD 50
sum       DWORD 0

.code
main PROC
    call Clrscr
     mov edx, offset myMessage
    call WriteString    
     mov ecx, arraysize
     xor ebx, ebx              ;quicker way to zero out a register
     mov eax, sum              ;register math is quicker than memory math
    .WHILE  ebx < ecx
        mov edx, array[ebx*4]  ;scale index to 4 bytes, size of dword
        .IF eax <= sample      ;is what in the eax/array <= sample
            add eax,edx        ;register math is faster than memory access
        .ENDIF
        inc ebx                ;index++    
    .ENDW
    push eax;                  ;store sum
    mov edx,offset myMess2
    call WriteString           ;display sum message
    pop edi                    ;restore sum to edi
    call WriteInt
    call Crlf   
    exit
main ENDP
END main

 
HotCode (Programmer)
30 Sep 11 9:34
Thank you for the advice I was still thinking in Java, that just slipped my mind about running though the addresses not doing like java, at least I was not off by 1 but 4.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close