Calculate the sum of all high-order bytes in array NUM1

In summary: ENDPIn summary, the author attempted to solve a homework problem, but made a mistake. They included the Irvine32.inc file and wrote code to calculate the sum of all high-order bytes in an array. They defined a new variable named newH and initialized it to 0. They then executed the main function.
  • #1
Fatima Hasan
319
14

Homework Statement


Calculate the sum of all hight-order bytes in array NUM1 and store the sum in a memory location named newH. Define newH as needed.

Homework Equations


-

The Attempt at a Solution


Code:
INCLUDE Irvine32.inc
.data
NUM1 sword 1h,2h,3h,4h,5h,6h,7h,8h,9h,10h,11h,12h,13h,14h,15h,16h
newH sdword 0

.code
main PROC 
; The sum of all hight-order bytes in array NUM1
    mov ecx,16
    mov esi,0
L2: movsx eax,NUM1[2*esi+1]
    add newH,eax
    inc esi
    loop L2

exit
main ENDP

END main
Can somebody tell me where is my mistake ?
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Can't you run this code in a debugger and find out where it went wrong?

You can run this code inside Visual Studio if you have it:

http://www.oneonta.edu/faculty/higgindm/assembly/using_visual_studio_to_assemble.htm

The statements I would look at closely are:
Code:
L2: movsx eax,NUM1[2*esi+1]
add newH,eax

My questions would be:
- what data NUM1[2*esi+1] pointing to?
- what data is loaded into eax?
---- is the value of NUM1[2*esi+1]?
---- or the data that is pointed to by NUM1[2*esi+1]?
- does add newH, eax add the contents of eax to newH is it really adding the value of NUM1[2*esi+1]?

As a programmer, you must be suspicious of all of your code? and after a while, you learn how decide the most suspicious lines?

and to me as a programmer, the most suspicious line is the movsx eax,NUM1[2*esi+1]
 
Last edited:
  • #3
jedishrfu said:
Can't you run this code in a debugger and find out where it went wrong?

You can run this code inside Visual Studio if you have it:

http://www.oneonta.edu/faculty/higgindm/assembly/using_visual_studio_to_assemble.htm

The statements I would look at closely are:
Code:
L2: movsx eax,NUM1[2*esi+1]
add newH,eax

My questions would be:
- what data NUM1[2*esi+1] pointing to?
- what data is loaded into eax?
---- is the value of NUM1[2*esi+1]?
---- or the data that is pointed to by NUM1[2*esi+1]?
- does add newH, eax add the contents of eax to newH is it really adding the value of NUM1[2*esi+1]?

As a programmer, you must be suspicious of all of your code? and after a while, you learn how decide the most suspicious lines?

and to me as a programmer, the most suspicious line is the movsx eax,NUM1[2*esi+1]
INCLUDE Irvine32.inc
.data
NUM1 sword 1h,2h,3h,4h,5h,6h,7h,8h,9h,10h,11h,12h,13h,14h,15h,16h
newH sword 0

.code
main PROC
; The sum of all hight-order bytes in array NUM1
mov ecx,16
mov esi,0
L2: movsx ax, byte ptr NUM1[2*esi+1]
add newH,ax
inc esi
loop L2

exit
main ENDP

END main
+ what about the size of newH
 
  • #4
when you sum the values you've provided do they exceed the size of newH?

Did you find a debugger to help you?
 
  • #5
jedishrfu said:
when you sum the values you've provided do they exceed the size of newH?

Did you find a debugger to help you?
I wrote the size of newH as a word because when we will calculate the hight order byte (byte+byte =word)
I know how to solve this if he ask to store the sum in register(such as: ax).. but my problem is when he asked to store the sum in a variable.. and i don’t know which size should i use .
 
  • #6
Fatima Hasan said:

Homework Statement


Calculate the sum of all hight-order bytes in array NUM1 and store the sum in a memory location named newH. Define newH as needed.

Homework Equations


-

The Attempt at a Solution


Code:
INCLUDE Irvine32.inc
.data
NUM1 sword 1h,2h,3h,4h,5h,6h,7h,8h,9h,10h,11h,12h,13h,14h,15h,16h
newH sdword 0

.code
main PROC
; The sum of all hight-order bytes in array NUM1
    mov ecx,16
    mov esi,0
L2: movsx eax,NUM1[2*esi+1]
    add newH,eax
    inc esi
    loop L2

exit
main ENDP

END main
Can somebody tell me where is my mistake ?
Did you get a result of 0? The numbers in your array NUM1 are two-byte quantities with the high byte already set to zero, so you should get 0 + 0 + 0 + ... + 0 = 0 for your answer.

Here's some code I wrote as embedded assembly in a C main function, using a different set of numbers in the array. I also changed the movsx you were using (move with sign extension) to just plain mov. All the numbers in the array are positive, so there's no difference in this case between movsx and mov.

The result I get is 52 for the sum of the high-order bytes.

Also, @jedishrfu's advice of using a debugger is spot on. You really need to use a debugger when you're writing assembly code.
C:
int main(void)
{
   short NUM1[] = { 0x101, 0x202, 0x303, 0x404, 0x505, 0x606, 0x707, 0x808, 0x909, 0x110, 0x111, 0x112, 0x113, 0x114, 0x115, 0x116 };
   short newH = 0;
 
   _asm
  { 

      mov cx, 16
      mov esi, 0
      mov ax, 0 
   L2:
      mov al, byte ptr NUM1[2*esi +1]
      add newH, ax
      inc si
      loop L2
   }
   return 0;

}
 
  • #7
Mark44 said:
Did you get a result of 0? The numbers in your array NUM1 are two-byte quantities with the high byte already set to zero, so you should get 0 + 0 + 0 + ... + 0 = 0 for your answer.

Here's some code I wrote as embedded assembly in a C main function, using a different set of numbers in the array. I also changed the movsx you were using (move with sign extension) to just plain mov. All the numbers in the array are positive, so there's no difference in this case between movsx and mov.

The result I get is 52 for the sum of the high-order bytes.

Also, @jedishrfu's advice of using a debugger is spot on. You really need to use a debugger when you're writing assembly code.
C:
int main(void)
{
   short NUM1[] = { 0x101, 0x202, 0x303, 0x404, 0x505, 0x606, 0x707, 0x808, 0x909, 0x110, 0x111, 0x112, 0x113, 0x114, 0x115, 0x116 };
   short newH = 0;
 
   _asm
  {

      mov cx, 16
      mov esi, 0
      mov ax, 0
   L2:
      mov al, byte ptr NUM1[2*esi +1]
      add newH, ax
      inc si
      loop L2
   }
   return 0;

}
INCLUDE Irvine32.inc
.data
NUM1 sword 1h,2h,3h,4h,5h,6h,7h,8h,9h,10h,11h,12h,13h,14h,15h,16h
newH sword 0

.code
main PROC
; The sum of all hight-order bytes in array NUM1
mov ecx,16
mov esi,0

L2: movsx ax,byte ptr NUM1[2*esi+1]
add newH,ax
inc esi
loop L2
exit
main ENDP
END main
 
Last edited by a moderator:
  • #8
Fatima Hasan said:
INCLUDE Irvine32.inc
.data
NUM1 sword 1h,2h,3h,4h,5h,6h,7h,8h,9h,10h,11h,12h,13h,14h,15h,16h
newH sword 0

.code
main PROC
; The sum of all hight-order bytes in array NUM1
mov ecx,16
mov esi,0

L2: movsx ax,byte ptr NUM1[2*esi+1]
add newH,ax
inc esi
loop L2
exit
main ENDP
END main
Isn't this the same code you posted earlier?
Did you read what I wrote?
Since all the high-order bytes in your array, NUM1, are zero, you're going to get zero again when you add them all together.

Edit: I see that you made a small change in the original code, adding "byte ptr" in the line with the label.
 
Last edited:
  • #9
Mark44 said:
Isn't this the same code you posted earlier?
Did you read what I wrote?
Since all the high-order bytes in your array, NUM1, are zero, you're going to get zero again when you add them all together.

Edit: I see that you made a small change in the original code, adding "byte ptr" in the line with the label.
Because I’m sure about this answer is current if the question is : store the sum of hight order bytes of array NUM1 in ax register. So, i just declare variable and store the sum in NUMX rather than ax register . I think it's should be same just change the register (ax) to variable

.data

NUM1 sword 1h,2h,3h,4h,5h,6h,7h,8h,9h,10h,11h,12h,13h,14h,15h,16h
.code

main PROC

; The sum of all hight-order bytes in ax register

mov ecx,16
mov esi,0
mov ax,0

L2: movsx bx,byte ptr NUM1[2*esi+1]

add ax,bx
inc esi
loop L2
exit
main ENDP
END main
 
  • #10
Fatima Hasan said:
Because I’m sure about this answer is current if the question is : store the sum of hight order bytes of array NUM1 in ax register.
  1. The values in your array are not a good test for your code -- all of the high-order bytes are 0, so you won't be able to tell whether your code is working or not.
  2. "hight" is not a word in English. The word you're looking for is "high."
  3. In post #1, copied below, it says to store the sum in a memory location named newH.
    Fatima Hasan said:
    store the sum in a memory location named newH. Define newH as needed.
    So are you supposed to store the value in a variable or in the ax register?
 
Last edited:

1. What is the purpose of calculating the sum of all high-order bytes in array NUM1?

The purpose of calculating the sum of all high-order bytes in array NUM1 is to determine the total value of all the most significant bytes in the array. This can be useful in various applications such as data compression, error detection, and numerical analysis.

2. How do you calculate the sum of all high-order bytes in array NUM1?

To calculate the sum of all high-order bytes in array NUM1, you would need to iterate through the array and extract the most significant byte from each element. Then, you can add all of these bytes together to get the total sum.

3. Can the sum of all high-order bytes in array NUM1 be negative?

It is possible for the sum of all high-order bytes in array NUM1 to be negative. This can happen if the array contains a mix of positive and negative numbers, or if the sum exceeds the maximum value that can be represented by the data type used to store the sum.

4. What is the significance of the high-order bytes in an array?

The high-order bytes in an array represent the most significant bits of each element. These bits have a greater impact on the overall value of the data and are often used for important calculations and operations.

5. Are there any special considerations when calculating the sum of all high-order bytes in array NUM1?

One special consideration when calculating the sum of all high-order bytes in array NUM1 is to ensure that the data type used to store the sum is large enough to hold the total value. If the sum exceeds the maximum value for the data type, it may result in unexpected errors or incorrect calculations.

Back
Top