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
AI Thread Summary
The discussion focuses on calculating the sum of high-order bytes from an array named NUM1 in assembly language. Participants highlight issues with the original code, particularly the use of `movsx` and the data types involved, noting that the high-order bytes in NUM1 are zero, leading to a sum of zero. Suggestions include using a debugger to identify errors and verifying the data being accessed. There is confusion about whether to store the sum in a variable or a register, with emphasis on ensuring the correct data type for the sum. Ultimately, the importance of testing with appropriate data is underscored to validate the code's functionality.
Fatima Hasan
Messages
315
Reaction score
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
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 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
 
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 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 .
 
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;

}
 
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:
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:
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:
Back
Top