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

  • Thread starter Thread starter Fatima Hasan
  • Start date Start date
  • Tags Tags
    Array bytes Sum
Click For Summary

Discussion Overview

The discussion revolves around calculating the sum of all high-order bytes in an array named NUM1 and storing the result in a memory location called newH. Participants explore various aspects of assembly language coding, debugging techniques, and the implications of data types and sizes in this context.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents an assembly code snippet to sum high-order bytes from the NUM1 array and questions the correctness of their implementation.
  • Another participant suggests using a debugger to identify potential issues in the code, particularly focusing on the lines that manipulate NUM1 and newH.
  • Concerns are raised about whether the high-order bytes in NUM1 are being correctly accessed and summed, with questions about the data being loaded into registers.
  • Some participants note that the high-order bytes in the provided NUM1 array are zero, leading to a sum of zero, which may not be a useful test for the code's functionality.
  • There is a discussion about the appropriate size for newH, with one participant arguing that it should be a word due to the nature of the operation, while another suggests it should be a double word to accommodate larger sums.
  • One participant shares an alternative code example using different numbers in the array, resulting in a non-zero sum, and emphasizes the importance of using a debugger.
  • Another participant points out that changing the register used for storing the sum does not change the requirement to store the result in newH as specified in the original problem statement.

Areas of Agreement / Disagreement

Participants express differing views on the correctness of the original code, the implications of using zero values in the array, and the appropriate data type for newH. There is no consensus on the best approach to resolve the issues raised.

Contextual Notes

Limitations include potential misunderstandings regarding the data types and sizes of variables, as well as the specific requirements for storing results in memory versus registers. The discussion also highlights the need for clarity in problem statements, particularly regarding terminology such as "height" versus "high."

Who May Find This Useful

This discussion may be useful for students and practitioners of assembly language programming, particularly those interested in debugging techniques and understanding data manipulation in low-level programming contexts.

Fatima Hasan
Messages
315
Reaction score
14

Homework Statement


Calculate the sum of all height-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 height-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
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:
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 height-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
 
when you sum the values you've provided do they exceed the size of newH?

Did you find a debugger to help you?
 
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 height 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 .
 
Fatima Hasan said:

Homework Statement


Calculate the sum of all height-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 height-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;

}
 
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 height-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:
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 height-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:
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 height 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 height-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 height 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. "height" 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: